cmGeneratorTarget.cxx 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307
  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 <algorithm>
  5. #include <array>
  6. #include <cassert>
  7. #include <cerrno>
  8. #include <cstddef>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <iterator>
  13. #include <queue>
  14. #include <sstream>
  15. #include <type_traits>
  16. #include <unordered_set>
  17. #include <utility>
  18. #include <cm/memory>
  19. #include <cm/optional>
  20. #include <cm/string_view>
  21. #include <cmext/algorithm>
  22. #include <cmext/string_view>
  23. #include "cmsys/RegularExpression.hxx"
  24. #include "cmAlgorithms.h"
  25. #include "cmComputeLinkInformation.h"
  26. #include "cmCryptoHash.h"
  27. #include "cmCustomCommandGenerator.h"
  28. #include "cmCxxModuleUsageEffects.h"
  29. #include "cmEvaluatedTargetProperty.h"
  30. #include "cmFileSet.h"
  31. #include "cmFileTimes.h"
  32. #include "cmGeneratedFileStream.h"
  33. #include "cmGeneratorExpression.h"
  34. #include "cmGeneratorExpressionContext.h"
  35. #include "cmGeneratorExpressionDAGChecker.h"
  36. #include "cmGeneratorExpressionNode.h"
  37. #include "cmGlobalGenerator.h"
  38. #include "cmList.h"
  39. #include "cmLocalGenerator.h"
  40. #include "cmMakefile.h"
  41. #include "cmMessageType.h"
  42. #include "cmOutputConverter.h"
  43. #include "cmPropertyMap.h"
  44. #include "cmRange.h"
  45. #include "cmSourceFile.h"
  46. #include "cmSourceFileLocation.h"
  47. #include "cmSourceFileLocationKind.h"
  48. #include "cmSourceGroup.h"
  49. #include "cmStandardLevelResolver.h"
  50. #include "cmState.h"
  51. #include "cmStringAlgorithms.h"
  52. #include "cmSyntheticTargetCache.h"
  53. #include "cmSystemTools.h"
  54. #include "cmTarget.h"
  55. #include "cmTargetLinkLibraryType.h"
  56. #include "cmTargetPropertyComputer.h"
  57. #include "cmake.h"
  58. namespace {
  59. using LinkInterfaceFor = cmGeneratorTarget::LinkInterfaceFor;
  60. const std::string kINTERFACE_LINK_LIBRARIES = "INTERFACE_LINK_LIBRARIES";
  61. const std::string kINTERFACE_LINK_LIBRARIES_DIRECT =
  62. "INTERFACE_LINK_LIBRARIES_DIRECT";
  63. const std::string kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE =
  64. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE";
  65. }
  66. template <>
  67. cmValue cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  68. cmGeneratorTarget const* tgt, cmMakefile const& /* mf */)
  69. {
  70. return tgt->GetSourcesProperty();
  71. }
  72. template <>
  73. const std::string&
  74. cmTargetPropertyComputer::ComputeLocationForBuild<cmGeneratorTarget>(
  75. cmGeneratorTarget const* tgt)
  76. {
  77. return tgt->GetLocation("");
  78. }
  79. template <>
  80. const std::string&
  81. cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  82. cmGeneratorTarget const* tgt, const std::string& config)
  83. {
  84. return tgt->GetLocation(config);
  85. }
  86. cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;
  87. class TargetPropertyEntryString : public cmGeneratorTarget::TargetPropertyEntry
  88. {
  89. public:
  90. TargetPropertyEntryString(BT<std::string> propertyValue,
  91. cmLinkImplItem const& item = NoLinkImplItem)
  92. : cmGeneratorTarget::TargetPropertyEntry(item)
  93. , PropertyValue(std::move(propertyValue))
  94. {
  95. }
  96. const std::string& Evaluate(cmLocalGenerator*, const std::string&,
  97. cmGeneratorTarget const*,
  98. cmGeneratorExpressionDAGChecker*,
  99. std::string const&) const override
  100. {
  101. return this->PropertyValue.Value;
  102. }
  103. cmListFileBacktrace GetBacktrace() const override
  104. {
  105. return this->PropertyValue.Backtrace;
  106. }
  107. std::string const& GetInput() const override
  108. {
  109. return this->PropertyValue.Value;
  110. }
  111. private:
  112. BT<std::string> PropertyValue;
  113. };
  114. class TargetPropertyEntryGenex : public cmGeneratorTarget::TargetPropertyEntry
  115. {
  116. public:
  117. TargetPropertyEntryGenex(std::unique_ptr<cmCompiledGeneratorExpression> cge,
  118. cmLinkImplItem const& item = NoLinkImplItem)
  119. : cmGeneratorTarget::TargetPropertyEntry(item)
  120. , ge(std::move(cge))
  121. {
  122. }
  123. const std::string& Evaluate(cmLocalGenerator* lg, const std::string& config,
  124. cmGeneratorTarget const* headTarget,
  125. cmGeneratorExpressionDAGChecker* dagChecker,
  126. std::string const& language) const override
  127. {
  128. return this->ge->Evaluate(lg, config, headTarget, dagChecker, nullptr,
  129. language);
  130. }
  131. cmListFileBacktrace GetBacktrace() const override
  132. {
  133. return this->ge->GetBacktrace();
  134. }
  135. std::string const& GetInput() const override { return this->ge->GetInput(); }
  136. bool GetHadContextSensitiveCondition() const override
  137. {
  138. return this->ge->GetHadContextSensitiveCondition();
  139. }
  140. private:
  141. const std::unique_ptr<cmCompiledGeneratorExpression> ge;
  142. };
  143. class TargetPropertyEntryFileSet
  144. : public cmGeneratorTarget::TargetPropertyEntry
  145. {
  146. public:
  147. TargetPropertyEntryFileSet(
  148. std::vector<std::string> dirs, bool contextSensitiveDirs,
  149. std::unique_ptr<cmCompiledGeneratorExpression> entryCge,
  150. const cmFileSet* fileSet, cmLinkImplItem const& item = NoLinkImplItem)
  151. : cmGeneratorTarget::TargetPropertyEntry(item)
  152. , BaseDirs(std::move(dirs))
  153. , ContextSensitiveDirs(contextSensitiveDirs)
  154. , EntryCge(std::move(entryCge))
  155. , FileSet(fileSet)
  156. {
  157. }
  158. const std::string& Evaluate(cmLocalGenerator* lg, const std::string& config,
  159. cmGeneratorTarget const* headTarget,
  160. cmGeneratorExpressionDAGChecker* dagChecker,
  161. std::string const& /*lang*/) const override
  162. {
  163. std::map<std::string, std::vector<std::string>> filesPerDir;
  164. this->FileSet->EvaluateFileEntry(this->BaseDirs, filesPerDir,
  165. this->EntryCge, lg, config, headTarget,
  166. dagChecker);
  167. std::vector<std::string> files;
  168. for (auto const& it : filesPerDir) {
  169. files.insert(files.end(), it.second.begin(), it.second.end());
  170. }
  171. static std::string filesStr;
  172. filesStr = cmList::to_string(files);
  173. return filesStr;
  174. }
  175. cmListFileBacktrace GetBacktrace() const override
  176. {
  177. return this->EntryCge->GetBacktrace();
  178. }
  179. std::string const& GetInput() const override
  180. {
  181. return this->EntryCge->GetInput();
  182. }
  183. bool GetHadContextSensitiveCondition() const override
  184. {
  185. return this->ContextSensitiveDirs ||
  186. this->EntryCge->GetHadContextSensitiveCondition();
  187. }
  188. private:
  189. const std::vector<std::string> BaseDirs;
  190. const bool ContextSensitiveDirs;
  191. const std::unique_ptr<cmCompiledGeneratorExpression> EntryCge;
  192. const cmFileSet* FileSet;
  193. };
  194. std::unique_ptr<
  195. cmGeneratorTarget::
  196. TargetPropertyEntry> static CreateTargetPropertyEntry(cmake& cmakeInstance,
  197. const BT<
  198. std::string>&
  199. propertyValue,
  200. bool
  201. evaluateForBuildsystem =
  202. false)
  203. {
  204. if (cmGeneratorExpression::Find(propertyValue.Value) != std::string::npos) {
  205. cmGeneratorExpression ge(cmakeInstance, propertyValue.Backtrace);
  206. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  207. ge.Parse(propertyValue.Value);
  208. cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
  209. return std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>(
  210. cm::make_unique<TargetPropertyEntryGenex>(std::move(cge)));
  211. }
  212. return std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>(
  213. cm::make_unique<TargetPropertyEntryString>(propertyValue));
  214. }
  215. cmGeneratorTarget::TargetPropertyEntry::TargetPropertyEntry(
  216. cmLinkImplItem const& item)
  217. : LinkImplItem(item)
  218. {
  219. }
  220. bool cmGeneratorTarget::TargetPropertyEntry::GetHadContextSensitiveCondition()
  221. const
  222. {
  223. return false;
  224. }
  225. static void CreatePropertyGeneratorExpressions(
  226. cmake& cmakeInstance, cmBTStringRange entries,
  227. std::vector<std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>>& items,
  228. bool evaluateForBuildsystem = false)
  229. {
  230. for (auto const& entry : entries) {
  231. items.push_back(
  232. CreateTargetPropertyEntry(cmakeInstance, entry, evaluateForBuildsystem));
  233. }
  234. }
  235. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
  236. : Target(t)
  237. {
  238. this->Makefile = this->Target->GetMakefile();
  239. this->LocalGenerator = lg;
  240. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  241. this->GlobalGenerator->ComputeTargetObjectDirectory(this);
  242. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  243. t->GetIncludeDirectoriesEntries(),
  244. this->IncludeDirectoriesEntries);
  245. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  246. t->GetCompileOptionsEntries(),
  247. this->CompileOptionsEntries);
  248. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  249. t->GetCompileFeaturesEntries(),
  250. this->CompileFeaturesEntries);
  251. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  252. t->GetCompileDefinitionsEntries(),
  253. this->CompileDefinitionsEntries);
  254. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  255. t->GetLinkOptionsEntries(),
  256. this->LinkOptionsEntries);
  257. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  258. t->GetLinkDirectoriesEntries(),
  259. this->LinkDirectoriesEntries);
  260. CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
  261. t->GetPrecompileHeadersEntries(),
  262. this->PrecompileHeadersEntries);
  263. CreatePropertyGeneratorExpressions(
  264. *lg->GetCMakeInstance(), t->GetSourceEntries(), this->SourceEntries, true);
  265. this->PolicyMap = t->GetPolicyMap();
  266. // Get hard-coded linker language
  267. if (this->Target->GetProperty("HAS_CXX")) {
  268. this->LinkerLanguage = "CXX";
  269. } else {
  270. this->LinkerLanguage = this->Target->GetSafeProperty("LINKER_LANGUAGE");
  271. }
  272. }
  273. cmGeneratorTarget::~cmGeneratorTarget() = default;
  274. cmValue cmGeneratorTarget::GetSourcesProperty() const
  275. {
  276. std::vector<std::string> values;
  277. for (auto const& se : this->SourceEntries) {
  278. values.push_back(se->GetInput());
  279. }
  280. static std::string value;
  281. value = cmList::to_string(values);
  282. return cmValue(value);
  283. }
  284. cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
  285. {
  286. return this->GetLocalGenerator()->GetGlobalGenerator();
  287. }
  288. cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
  289. {
  290. return this->LocalGenerator;
  291. }
  292. cmStateEnums::TargetType cmGeneratorTarget::GetType() const
  293. {
  294. return this->Target->GetType();
  295. }
  296. const std::string& cmGeneratorTarget::GetName() const
  297. {
  298. return this->Target->GetName();
  299. }
  300. std::string cmGeneratorTarget::GetExportName() const
  301. {
  302. cmValue exportName = this->GetProperty("EXPORT_NAME");
  303. if (cmNonempty(exportName)) {
  304. if (!cmGeneratorExpression::IsValidTargetName(*exportName)) {
  305. std::ostringstream e;
  306. e << "EXPORT_NAME property \"" << *exportName << "\" for \""
  307. << this->GetName() << "\": is not valid.";
  308. cmSystemTools::Error(e.str());
  309. return "";
  310. }
  311. return *exportName;
  312. }
  313. return this->GetName();
  314. }
  315. cmValue cmGeneratorTarget::GetProperty(const std::string& prop) const
  316. {
  317. if (cmValue result =
  318. cmTargetPropertyComputer::GetProperty(this, prop, *this->Makefile)) {
  319. return result;
  320. }
  321. if (cmSystemTools::GetFatalErrorOccurred()) {
  322. return nullptr;
  323. }
  324. return this->Target->GetProperty(prop);
  325. }
  326. std::string const& cmGeneratorTarget::GetSafeProperty(
  327. std::string const& prop) const
  328. {
  329. return this->GetProperty(prop);
  330. }
  331. const char* cmGeneratorTarget::GetOutputTargetType(
  332. cmStateEnums::ArtifactType artifact) const
  333. {
  334. if (this->IsFrameworkOnApple() || this->GetGlobalGenerator()->IsXcode()) {
  335. // import file (i.e. .tbd file) is always in same location as library
  336. artifact = cmStateEnums::RuntimeBinaryArtifact;
  337. }
  338. switch (this->GetType()) {
  339. case cmStateEnums::SHARED_LIBRARY:
  340. if (this->IsDLLPlatform()) {
  341. switch (artifact) {
  342. case cmStateEnums::RuntimeBinaryArtifact:
  343. // A DLL shared library is treated as a runtime target.
  344. return "RUNTIME";
  345. case cmStateEnums::ImportLibraryArtifact:
  346. // A DLL import library is treated as an archive target.
  347. return "ARCHIVE";
  348. }
  349. } else {
  350. switch (artifact) {
  351. case cmStateEnums::RuntimeBinaryArtifact:
  352. // For non-DLL platforms shared libraries are treated as
  353. // library targets.
  354. return "LIBRARY";
  355. case cmStateEnums::ImportLibraryArtifact:
  356. // Library import libraries are treated as archive targets.
  357. return "ARCHIVE";
  358. }
  359. }
  360. break;
  361. case cmStateEnums::STATIC_LIBRARY:
  362. // Static libraries are always treated as archive targets.
  363. return "ARCHIVE";
  364. case cmStateEnums::MODULE_LIBRARY:
  365. switch (artifact) {
  366. case cmStateEnums::RuntimeBinaryArtifact:
  367. // Module libraries are always treated as library targets.
  368. return "LIBRARY";
  369. case cmStateEnums::ImportLibraryArtifact:
  370. // Module import libraries are treated as archive targets.
  371. return "ARCHIVE";
  372. }
  373. break;
  374. case cmStateEnums::OBJECT_LIBRARY:
  375. // Object libraries are always treated as object targets.
  376. return "OBJECT";
  377. case cmStateEnums::EXECUTABLE:
  378. switch (artifact) {
  379. case cmStateEnums::RuntimeBinaryArtifact:
  380. // Executables are always treated as runtime targets.
  381. return "RUNTIME";
  382. case cmStateEnums::ImportLibraryArtifact:
  383. // Executable import libraries are treated as archive targets.
  384. return "ARCHIVE";
  385. }
  386. break;
  387. default:
  388. break;
  389. }
  390. return "";
  391. }
  392. std::string cmGeneratorTarget::GetOutputName(
  393. const std::string& config, cmStateEnums::ArtifactType artifact) const
  394. {
  395. // Lookup/compute/cache the output name for this configuration.
  396. OutputNameKey key(config, artifact);
  397. auto i = this->OutputNameMap.find(key);
  398. if (i == this->OutputNameMap.end()) {
  399. // Add empty name in map to detect potential recursion.
  400. OutputNameMapType::value_type entry(key, "");
  401. i = this->OutputNameMap.insert(entry).first;
  402. // Compute output name.
  403. std::vector<std::string> props;
  404. std::string type = this->GetOutputTargetType(artifact);
  405. std::string configUpper = cmSystemTools::UpperCase(config);
  406. if (!type.empty() && !configUpper.empty()) {
  407. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
  408. props.push_back(type + "_OUTPUT_NAME_" + configUpper);
  409. }
  410. if (!type.empty()) {
  411. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
  412. props.push_back(type + "_OUTPUT_NAME");
  413. }
  414. if (!configUpper.empty()) {
  415. // OUTPUT_NAME_<CONFIG>
  416. props.push_back("OUTPUT_NAME_" + configUpper);
  417. // <CONFIG>_OUTPUT_NAME
  418. props.push_back(configUpper + "_OUTPUT_NAME");
  419. }
  420. // OUTPUT_NAME
  421. props.emplace_back("OUTPUT_NAME");
  422. std::string outName;
  423. for (std::string const& p : props) {
  424. if (cmValue outNameProp = this->GetProperty(p)) {
  425. outName = *outNameProp;
  426. break;
  427. }
  428. }
  429. if (outName.empty()) {
  430. outName = this->GetName();
  431. }
  432. // Now evaluate genex and update the previously-prepared map entry.
  433. i->second =
  434. cmGeneratorExpression::Evaluate(outName, this->LocalGenerator, config);
  435. } else if (i->second.empty()) {
  436. // An empty map entry indicates we have been called recursively
  437. // from the above block.
  438. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  439. MessageType::FATAL_ERROR,
  440. "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
  441. this->GetBacktrace());
  442. }
  443. return i->second;
  444. }
  445. std::string cmGeneratorTarget::GetFilePrefix(
  446. const std::string& config, cmStateEnums::ArtifactType artifact) const
  447. {
  448. if (this->IsImported()) {
  449. cmValue prefix = this->GetFilePrefixInternal(config, artifact);
  450. return prefix ? *prefix : std::string();
  451. }
  452. return this->GetFullNameInternalComponents(config, artifact).prefix;
  453. }
  454. std::string cmGeneratorTarget::GetFileSuffix(
  455. const std::string& config, cmStateEnums::ArtifactType artifact) const
  456. {
  457. if (this->IsImported()) {
  458. cmValue suffix = this->GetFileSuffixInternal(config, artifact);
  459. return suffix ? *suffix : std::string();
  460. }
  461. return this->GetFullNameInternalComponents(config, artifact).suffix;
  462. }
  463. std::string cmGeneratorTarget::GetFilePostfix(const std::string& config) const
  464. {
  465. cmValue postfix = nullptr;
  466. std::string frameworkPostfix;
  467. if (!config.empty()) {
  468. std::string configProp =
  469. cmStrCat(cmSystemTools::UpperCase(config), "_POSTFIX");
  470. postfix = this->GetProperty(configProp);
  471. // Mac application bundles and frameworks have no regular postfix like
  472. // libraries do.
  473. if (!this->IsImported() && postfix &&
  474. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  475. postfix = nullptr;
  476. }
  477. // Frameworks created by multi config generators can have a special
  478. // framework postfix.
  479. frameworkPostfix = this->GetFrameworkMultiConfigPostfix(config);
  480. if (!frameworkPostfix.empty()) {
  481. postfix = cmValue(frameworkPostfix);
  482. }
  483. }
  484. return postfix ? *postfix : std::string();
  485. }
  486. std::string cmGeneratorTarget::GetFrameworkMultiConfigPostfix(
  487. const std::string& config) const
  488. {
  489. cmValue postfix = nullptr;
  490. if (!config.empty()) {
  491. std::string configProp = cmStrCat("FRAMEWORK_MULTI_CONFIG_POSTFIX_",
  492. cmSystemTools::UpperCase(config));
  493. postfix = this->GetProperty(configProp);
  494. if (!this->IsImported() && postfix &&
  495. (this->IsFrameworkOnApple() &&
  496. !this->GetGlobalGenerator()->IsMultiConfig())) {
  497. postfix = nullptr;
  498. }
  499. }
  500. return postfix ? *postfix : std::string();
  501. }
  502. cmValue cmGeneratorTarget::GetFilePrefixInternal(
  503. std::string const& config, cmStateEnums::ArtifactType artifact,
  504. const std::string& language) const
  505. {
  506. // no prefix for non-main target types.
  507. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  508. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  509. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  510. this->GetType() != cmStateEnums::EXECUTABLE) {
  511. return nullptr;
  512. }
  513. const bool isImportedLibraryArtifact =
  514. (artifact == cmStateEnums::ImportLibraryArtifact);
  515. // Return an empty prefix for the import library if this platform
  516. // does not support import libraries.
  517. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  518. return nullptr;
  519. }
  520. // The implib option is only allowed for shared libraries, module
  521. // libraries, and executables.
  522. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  523. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  524. this->GetType() != cmStateEnums::EXECUTABLE) {
  525. artifact = cmStateEnums::RuntimeBinaryArtifact;
  526. }
  527. // Compute prefix value.
  528. cmValue targetPrefix =
  529. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  530. : this->GetProperty("PREFIX"));
  531. if (!targetPrefix) {
  532. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  533. if (!language.empty() && cmNonempty(prefixVar)) {
  534. std::string langPrefix = cmStrCat(prefixVar, "_", language);
  535. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  536. }
  537. // if there is no prefix on the target nor specific language
  538. // use the cmake definition.
  539. if (!targetPrefix && prefixVar) {
  540. targetPrefix = this->Makefile->GetDefinition(prefixVar);
  541. }
  542. }
  543. return targetPrefix;
  544. }
  545. cmValue cmGeneratorTarget::GetFileSuffixInternal(
  546. std::string const& config, cmStateEnums::ArtifactType artifact,
  547. const std::string& language) const
  548. {
  549. // no suffix for non-main target types.
  550. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  551. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  552. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  553. this->GetType() != cmStateEnums::EXECUTABLE) {
  554. return nullptr;
  555. }
  556. const bool isImportedLibraryArtifact =
  557. (artifact == cmStateEnums::ImportLibraryArtifact);
  558. // Return an empty suffix for the import library if this platform
  559. // does not support import libraries.
  560. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  561. return nullptr;
  562. }
  563. // The implib option is only allowed for shared libraries, module
  564. // libraries, and executables.
  565. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  566. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  567. this->GetType() != cmStateEnums::EXECUTABLE) {
  568. artifact = cmStateEnums::RuntimeBinaryArtifact;
  569. }
  570. // Compute suffix value.
  571. cmValue targetSuffix =
  572. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  573. : this->GetProperty("SUFFIX"));
  574. if (!targetSuffix) {
  575. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  576. if (!language.empty() && cmNonempty(suffixVar)) {
  577. std::string langSuffix = cmStrCat(suffixVar, "_", language);
  578. targetSuffix = this->Makefile->GetDefinition(langSuffix);
  579. }
  580. // if there is no suffix on the target nor specific language
  581. // use the cmake definition.
  582. if (!targetSuffix && suffixVar) {
  583. targetSuffix = this->Makefile->GetDefinition(suffixVar);
  584. }
  585. }
  586. return targetSuffix;
  587. }
  588. void cmGeneratorTarget::ClearSourcesCache()
  589. {
  590. this->AllConfigSources.clear();
  591. this->KindedSourcesMap.clear();
  592. this->SourcesAreContextDependent = Tribool::Indeterminate;
  593. this->Objects.clear();
  594. this->VisitedConfigsForObjects.clear();
  595. this->LinkImplMap.clear();
  596. this->LinkImplUsageRequirementsOnlyMap.clear();
  597. this->IncludeDirectoriesCache.clear();
  598. this->CompileOptionsCache.clear();
  599. this->CompileDefinitionsCache.clear();
  600. this->PrecompileHeadersCache.clear();
  601. this->LinkOptionsCache.clear();
  602. this->LinkDirectoriesCache.clear();
  603. this->RuntimeBinaryFullNameCache.clear();
  604. this->ImportLibraryFullNameCache.clear();
  605. }
  606. void cmGeneratorTarget::ClearLinkInterfaceCache()
  607. {
  608. this->LinkInterfaceMap.clear();
  609. this->LinkInterfaceUsageRequirementsOnlyMap.clear();
  610. }
  611. void cmGeneratorTarget::AddSourceCommon(const std::string& src, bool before)
  612. {
  613. this->SourceEntries.insert(
  614. before ? this->SourceEntries.begin() : this->SourceEntries.end(),
  615. CreateTargetPropertyEntry(
  616. *this->LocalGenerator->GetCMakeInstance(),
  617. BT<std::string>(src, this->Makefile->GetBacktrace()), true));
  618. this->ClearSourcesCache();
  619. }
  620. void cmGeneratorTarget::AddSource(const std::string& src, bool before)
  621. {
  622. this->Target->AddSource(src, before);
  623. this->AddSourceCommon(src, before);
  624. }
  625. void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
  626. {
  627. this->Target->AddTracedSources(srcs);
  628. if (!srcs.empty()) {
  629. this->AddSourceCommon(cmJoin(srcs, ";"));
  630. }
  631. }
  632. void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
  633. bool before)
  634. {
  635. this->Target->InsertInclude(
  636. BT<std::string>(src, this->Makefile->GetBacktrace()), before);
  637. this->IncludeDirectoriesEntries.insert(
  638. before ? this->IncludeDirectoriesEntries.begin()
  639. : this->IncludeDirectoriesEntries.end(),
  640. CreateTargetPropertyEntry(
  641. *this->Makefile->GetCMakeInstance(),
  642. BT<std::string>(src, this->Makefile->GetBacktrace()), true));
  643. }
  644. void cmGeneratorTarget::AddSystemIncludeDirectory(std::string const& inc,
  645. std::string const& lang)
  646. {
  647. std::string config_upper;
  648. auto const& configs =
  649. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  650. for (auto const& config : configs) {
  651. std::string inc_with_config = inc;
  652. if (!config.empty()) {
  653. cmSystemTools::ReplaceString(inc_with_config, "$<CONFIG>", config);
  654. config_upper = cmSystemTools::UpperCase(config);
  655. }
  656. auto const& key = cmStrCat(config_upper, "/", lang);
  657. this->Target->AddSystemIncludeDirectories({ inc_with_config });
  658. this->SystemIncludesCache[key].emplace_back(inc_with_config);
  659. // SystemIncludesCache should be sorted so that binary search can be used
  660. std::sort(this->SystemIncludesCache[key].begin(),
  661. this->SystemIncludesCache[key].end());
  662. }
  663. }
  664. std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  665. cmSourceFile const* sf) const
  666. {
  667. auto i = this->SourceDepends.find(sf);
  668. if (i != this->SourceDepends.end()) {
  669. return &i->second.Depends;
  670. }
  671. return nullptr;
  672. }
  673. namespace {
  674. void handleSystemIncludesDep(cmLocalGenerator* lg,
  675. cmGeneratorTarget const* depTgt,
  676. const std::string& config,
  677. cmGeneratorTarget const* headTarget,
  678. cmGeneratorExpressionDAGChecker* dagChecker,
  679. cmList& result, bool excludeImported,
  680. std::string const& language)
  681. {
  682. if (cmValue dirs =
  683. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
  684. result.append(cmGeneratorExpression::Evaluate(
  685. *dirs, lg, config, headTarget, dagChecker, depTgt, language));
  686. }
  687. if (!depTgt->GetPropertyAsBool("SYSTEM")) {
  688. return;
  689. }
  690. if (depTgt->IsImported()) {
  691. if (excludeImported) {
  692. return;
  693. }
  694. if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
  695. return;
  696. }
  697. }
  698. if (cmValue dirs = depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
  699. result.append(cmGeneratorExpression::Evaluate(
  700. *dirs, lg, config, headTarget, dagChecker, depTgt, language));
  701. }
  702. if (depTgt->Target->IsFrameworkOnApple() ||
  703. depTgt->IsImportedFrameworkFolderOnApple(config)) {
  704. if (auto fwDescriptor = depTgt->GetGlobalGenerator()->SplitFrameworkPath(
  705. depTgt->GetLocation(config))) {
  706. result.push_back(fwDescriptor->Directory);
  707. result.push_back(fwDescriptor->GetFrameworkPath());
  708. }
  709. }
  710. }
  711. }
  712. /* clang-format off */
  713. #define IMPLEMENT_VISIT(KIND) \
  714. do { \
  715. KindedSources const& kinded = this->GetKindedSources(config); \
  716. for (SourceAndKind const& s : kinded.Sources) { \
  717. if (s.Kind == KIND) { \
  718. data.push_back(s.Source.Value); \
  719. } \
  720. } \
  721. } while (false)
  722. /* clang-format on */
  723. void cmGeneratorTarget::GetObjectSources(
  724. std::vector<cmSourceFile const*>& data, const std::string& config) const
  725. {
  726. IMPLEMENT_VISIT(SourceKindObjectSource);
  727. if (this->VisitedConfigsForObjects.count(config)) {
  728. return;
  729. }
  730. for (cmSourceFile const* it : data) {
  731. this->Objects[it];
  732. }
  733. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  734. this->VisitedConfigsForObjects.insert(config);
  735. }
  736. void cmGeneratorTarget::ComputeObjectMapping()
  737. {
  738. auto const& configs =
  739. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  740. std::set<std::string> configSet(configs.begin(), configs.end());
  741. if (configSet == this->VisitedConfigsForObjects) {
  742. return;
  743. }
  744. for (std::string const& c : configs) {
  745. std::vector<cmSourceFile const*> sourceFiles;
  746. this->GetObjectSources(sourceFiles, c);
  747. }
  748. }
  749. cmValue cmGeneratorTarget::GetFeature(const std::string& feature,
  750. const std::string& config) const
  751. {
  752. if (!config.empty()) {
  753. std::string featureConfig =
  754. cmStrCat(feature, '_', cmSystemTools::UpperCase(config));
  755. if (cmValue value = this->GetProperty(featureConfig)) {
  756. return value;
  757. }
  758. }
  759. if (cmValue value = this->GetProperty(feature)) {
  760. return value;
  761. }
  762. return this->LocalGenerator->GetFeature(feature, config);
  763. }
  764. const char* cmGeneratorTarget::GetLinkPIEProperty(
  765. const std::string& config) const
  766. {
  767. static std::string PICValue;
  768. PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
  769. "POSITION_INDEPENDENT_CODE", config);
  770. if (PICValue == "(unset)") {
  771. // POSITION_INDEPENDENT_CODE is not set
  772. return nullptr;
  773. }
  774. auto status = this->GetPolicyStatusCMP0083();
  775. return (status != cmPolicies::WARN && status != cmPolicies::OLD)
  776. ? PICValue.c_str()
  777. : nullptr;
  778. }
  779. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  780. std::string const& config) const
  781. {
  782. cmValue feature = this->GetFeature("INTERPROCEDURAL_OPTIMIZATION", config);
  783. if (!cmIsOn(feature)) {
  784. // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
  785. return false;
  786. }
  787. if (lang != "C" && lang != "CXX" && lang != "CUDA" && lang != "Fortran") {
  788. // We do not define IPO behavior for other languages.
  789. return false;
  790. }
  791. if (lang == "CUDA") {
  792. // CUDA IPO requires both CUDA_ARCHITECTURES and CUDA_SEPARABLE_COMPILATION
  793. if (cmIsOff(this->GetSafeProperty("CUDA_ARCHITECTURES")) ||
  794. cmIsOff(this->GetSafeProperty("CUDA_SEPARABLE_COMPILATION"))) {
  795. return false;
  796. }
  797. }
  798. cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
  799. if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
  800. if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
  801. return true;
  802. }
  803. if (this->PolicyReportedCMP0069) {
  804. // problem is already reported, no need to issue a message
  805. return false;
  806. }
  807. const bool in_try_compile =
  808. this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
  809. if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
  810. std::ostringstream w;
  811. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
  812. w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
  813. << "'" << this->GetName() << "'.";
  814. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  815. MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  816. this->PolicyReportedCMP0069 = true;
  817. }
  818. return false;
  819. }
  820. // Note: check consistency with messages from CheckIPOSupported
  821. const char* message = nullptr;
  822. if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
  823. message = "CMake doesn't support IPO for current compiler";
  824. } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
  825. "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
  826. message = "Compiler doesn't support IPO";
  827. } else if (!this->GlobalGenerator->IsIPOSupported()) {
  828. message = "CMake doesn't support IPO for current generator";
  829. }
  830. if (!message) {
  831. // No error/warning messages
  832. return true;
  833. }
  834. if (this->PolicyReportedCMP0069) {
  835. // problem is already reported, no need to issue a message
  836. return false;
  837. }
  838. this->PolicyReportedCMP0069 = true;
  839. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  840. MessageType::FATAL_ERROR, message, this->GetBacktrace());
  841. return false;
  842. }
  843. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  844. {
  845. this->ComputeObjectMapping();
  846. return this->Objects[file];
  847. }
  848. const char* cmGeneratorTarget::GetCustomObjectExtension() const
  849. {
  850. struct compiler_mode
  851. {
  852. std::string variable;
  853. std::string extension;
  854. };
  855. static std::array<compiler_mode, 4> const modes{
  856. { { "CUDA_PTX_COMPILATION", ".ptx" },
  857. { "CUDA_CUBIN_COMPILATION", ".cubin" },
  858. { "CUDA_FATBIN_COMPILATION", ".fatbin" },
  859. { "CUDA_OPTIX_COMPILATION", ".optixir" } }
  860. };
  861. std::string const& compiler =
  862. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  863. if (!compiler.empty()) {
  864. for (const auto& m : modes) {
  865. const bool has_extension = this->GetPropertyAsBool(m.variable);
  866. if (has_extension) {
  867. return m.extension.c_str();
  868. }
  869. }
  870. }
  871. return nullptr;
  872. }
  873. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  874. {
  875. this->ExplicitObjectName.insert(sf);
  876. }
  877. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  878. {
  879. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  880. auto it = this->ExplicitObjectName.find(file);
  881. return it != this->ExplicitObjectName.end();
  882. }
  883. BTs<std::string> const* cmGeneratorTarget::GetLanguageStandardProperty(
  884. std::string const& lang, std::string const& config) const
  885. {
  886. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  887. auto langStandardIter = this->LanguageStandardMap.find(key);
  888. if (langStandardIter != this->LanguageStandardMap.end()) {
  889. return &langStandardIter->second;
  890. }
  891. return this->Target->GetLanguageStandardProperty(
  892. cmStrCat(lang, "_STANDARD"));
  893. }
  894. cmValue cmGeneratorTarget::GetLanguageStandard(std::string const& lang,
  895. std::string const& config) const
  896. {
  897. BTs<std::string> const* languageStandard =
  898. this->GetLanguageStandardProperty(lang, config);
  899. if (languageStandard) {
  900. return cmValue(languageStandard->Value);
  901. }
  902. return nullptr;
  903. }
  904. cmValue cmGeneratorTarget::GetPropertyWithPairedLanguageSupport(
  905. std::string const& lang, const char* suffix) const
  906. {
  907. cmValue propertyValue = this->Target->GetProperty(cmStrCat(lang, suffix));
  908. if (!propertyValue) {
  909. // Check if we should use the value set by another language.
  910. if (lang == "OBJC") {
  911. propertyValue = this->GetPropertyWithPairedLanguageSupport("C", suffix);
  912. } else if (lang == "OBJCXX" || lang == "CUDA" || lang == "HIP") {
  913. propertyValue =
  914. this->GetPropertyWithPairedLanguageSupport("CXX", suffix);
  915. }
  916. }
  917. return propertyValue;
  918. }
  919. cmValue cmGeneratorTarget::GetLanguageExtensions(std::string const& lang) const
  920. {
  921. return this->GetPropertyWithPairedLanguageSupport(lang, "_EXTENSIONS");
  922. }
  923. bool cmGeneratorTarget::GetLanguageStandardRequired(
  924. std::string const& lang) const
  925. {
  926. return cmIsOn(
  927. this->GetPropertyWithPairedLanguageSupport(lang, "_STANDARD_REQUIRED"));
  928. }
  929. void cmGeneratorTarget::GetModuleDefinitionSources(
  930. std::vector<cmSourceFile const*>& data, const std::string& config) const
  931. {
  932. IMPLEMENT_VISIT(SourceKindModuleDefinition);
  933. }
  934. void cmGeneratorTarget::GetHeaderSources(
  935. std::vector<cmSourceFile const*>& data, const std::string& config) const
  936. {
  937. IMPLEMENT_VISIT(SourceKindHeader);
  938. }
  939. void cmGeneratorTarget::GetCxxModuleSources(
  940. std::vector<cmSourceFile const*>& data, const std::string& config) const
  941. {
  942. IMPLEMENT_VISIT(SourceKindCxxModuleSource);
  943. }
  944. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
  945. const std::string& config) const
  946. {
  947. IMPLEMENT_VISIT(SourceKindExtra);
  948. }
  949. void cmGeneratorTarget::GetCustomCommands(
  950. std::vector<cmSourceFile const*>& data, const std::string& config) const
  951. {
  952. IMPLEMENT_VISIT(SourceKindCustomCommand);
  953. }
  954. void cmGeneratorTarget::GetExternalObjects(
  955. std::vector<cmSourceFile const*>& data, const std::string& config) const
  956. {
  957. IMPLEMENT_VISIT(SourceKindExternalObject);
  958. }
  959. void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
  960. const std::string& config) const
  961. {
  962. IMPLEMENT_VISIT(SourceKindManifest);
  963. }
  964. std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
  965. {
  966. if (!this->UtilityItemsDone) {
  967. this->UtilityItemsDone = true;
  968. std::set<BT<std::pair<std::string, bool>>> const& utilities =
  969. this->GetUtilities();
  970. for (BT<std::pair<std::string, bool>> const& i : utilities) {
  971. if (cmGeneratorTarget* gt =
  972. this->LocalGenerator->FindGeneratorTargetToUse(i.Value.first)) {
  973. this->UtilityItems.insert(cmLinkItem(gt, i.Value.second, i.Backtrace));
  974. } else {
  975. this->UtilityItems.insert(
  976. cmLinkItem(i.Value.first, i.Value.second, i.Backtrace));
  977. }
  978. }
  979. }
  980. return this->UtilityItems;
  981. }
  982. const std::string& cmGeneratorTarget::GetLocation(
  983. const std::string& config) const
  984. {
  985. static std::string location;
  986. if (this->IsImported()) {
  987. location = this->Target->ImportedGetFullPath(
  988. config, cmStateEnums::RuntimeBinaryArtifact);
  989. } else {
  990. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  991. }
  992. return location;
  993. }
  994. cm::optional<std::string> cmGeneratorTarget::MaybeGetLocation(
  995. std::string const& config) const
  996. {
  997. cm::optional<std::string> location;
  998. if (cmGeneratorTarget::ImportInfo const* imp = this->GetImportInfo(config)) {
  999. if (!imp->Location.empty()) {
  1000. location = imp->Location;
  1001. }
  1002. } else {
  1003. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  1004. }
  1005. return location;
  1006. }
  1007. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  1008. const
  1009. {
  1010. return this->Target->GetPreBuildCommands();
  1011. }
  1012. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  1013. const
  1014. {
  1015. return this->Target->GetPreLinkCommands();
  1016. }
  1017. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  1018. const
  1019. {
  1020. return this->Target->GetPostBuildCommands();
  1021. }
  1022. void cmGeneratorTarget::AppendCustomCommandSideEffects(
  1023. std::set<cmGeneratorTarget const*>& sideEffects) const
  1024. {
  1025. if (!this->GetPreBuildCommands().empty() ||
  1026. !this->GetPreLinkCommands().empty() ||
  1027. !this->GetPostBuildCommands().empty()) {
  1028. sideEffects.insert(this);
  1029. } else {
  1030. for (auto const& source : this->GetAllConfigSources()) {
  1031. if (source.Source->GetCustomCommand() != nullptr) {
  1032. sideEffects.insert(this);
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. void cmGeneratorTarget::AppendLanguageSideEffects(
  1039. std::map<std::string, std::set<cmGeneratorTarget const*>>& sideEffects) const
  1040. {
  1041. static const std::set<cm::string_view> LANGS_WITH_NO_SIDE_EFFECTS = {
  1042. "C"_s, "CXX"_s, "OBJC"_s, "OBJCXX"_s, "ASM"_s, "CUDA"_s, "HIP"_s
  1043. };
  1044. for (auto const& lang : this->GetAllConfigCompileLanguages()) {
  1045. if (!LANGS_WITH_NO_SIDE_EFFECTS.count(lang)) {
  1046. sideEffects[lang].insert(this);
  1047. }
  1048. }
  1049. }
  1050. bool cmGeneratorTarget::IsInBuildSystem() const
  1051. {
  1052. if (this->IsImported()) {
  1053. return false;
  1054. }
  1055. switch (this->Target->GetType()) {
  1056. case cmStateEnums::EXECUTABLE:
  1057. case cmStateEnums::STATIC_LIBRARY:
  1058. case cmStateEnums::SHARED_LIBRARY:
  1059. case cmStateEnums::MODULE_LIBRARY:
  1060. case cmStateEnums::OBJECT_LIBRARY:
  1061. case cmStateEnums::UTILITY:
  1062. case cmStateEnums::GLOBAL_TARGET:
  1063. return true;
  1064. case cmStateEnums::INTERFACE_LIBRARY:
  1065. // An INTERFACE library is in the build system if it has SOURCES
  1066. // or C++ module filesets.
  1067. if (!this->SourceEntries.empty() ||
  1068. !this->Target->GetHeaderSetsEntries().empty() ||
  1069. !this->Target->GetCxxModuleSetsEntries().empty()) {
  1070. return true;
  1071. }
  1072. break;
  1073. case cmStateEnums::UNKNOWN_LIBRARY:
  1074. break;
  1075. }
  1076. return false;
  1077. }
  1078. bool cmGeneratorTarget::IsNormal() const
  1079. {
  1080. return this->Target->IsNormal();
  1081. }
  1082. bool cmGeneratorTarget::IsRuntimeBinary() const
  1083. {
  1084. return this->Target->IsRuntimeBinary();
  1085. }
  1086. bool cmGeneratorTarget::IsSynthetic() const
  1087. {
  1088. return this->Target->IsSynthetic();
  1089. }
  1090. bool cmGeneratorTarget::IsImported() const
  1091. {
  1092. return this->Target->IsImported();
  1093. }
  1094. bool cmGeneratorTarget::IsImportedGloballyVisible() const
  1095. {
  1096. return this->Target->IsImportedGloballyVisible();
  1097. }
  1098. bool cmGeneratorTarget::CanCompileSources() const
  1099. {
  1100. return this->Target->CanCompileSources();
  1101. }
  1102. const std::string& cmGeneratorTarget::GetLocationForBuild() const
  1103. {
  1104. static std::string location;
  1105. if (this->IsImported()) {
  1106. location = this->Target->ImportedGetFullPath(
  1107. "", cmStateEnums::RuntimeBinaryArtifact);
  1108. return location;
  1109. }
  1110. // Now handle the deprecated build-time configuration location.
  1111. std::string const noConfig;
  1112. location = this->GetDirectory(noConfig);
  1113. cmValue cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  1114. if (cfgid && (*cfgid != ".")) {
  1115. location += "/";
  1116. location += *cfgid;
  1117. }
  1118. if (this->IsAppBundleOnApple()) {
  1119. std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
  1120. if (!macdir.empty()) {
  1121. location += "/";
  1122. location += macdir;
  1123. }
  1124. }
  1125. location += "/";
  1126. location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
  1127. return location;
  1128. }
  1129. bool cmGeneratorTarget::IsSystemIncludeDirectory(
  1130. const std::string& dir, const std::string& config,
  1131. const std::string& language) const
  1132. {
  1133. std::string config_upper;
  1134. if (!config.empty()) {
  1135. config_upper = cmSystemTools::UpperCase(config);
  1136. }
  1137. std::string key = cmStrCat(config_upper, '/', language);
  1138. auto iter = this->SystemIncludesCache.find(key);
  1139. if (iter == this->SystemIncludesCache.end()) {
  1140. cmGeneratorExpressionDAGChecker dagChecker(
  1141. this, "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr);
  1142. bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  1143. cmList result;
  1144. for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
  1145. result.append(cmGeneratorExpression::Evaluate(it, this->LocalGenerator,
  1146. config, this, &dagChecker,
  1147. nullptr, language));
  1148. }
  1149. std::vector<cmGeneratorTarget const*> const& deps =
  1150. this->GetLinkImplementationClosure(config);
  1151. for (cmGeneratorTarget const* dep : deps) {
  1152. handleSystemIncludesDep(this->LocalGenerator, dep, config, this,
  1153. &dagChecker, result, excludeImported, language);
  1154. }
  1155. cmLinkImplementation const* impl =
  1156. this->GetLinkImplementation(config, LinkInterfaceFor::Usage);
  1157. if (impl != nullptr) {
  1158. auto runtimeEntries = impl->LanguageRuntimeLibraries.find(language);
  1159. if (runtimeEntries != impl->LanguageRuntimeLibraries.end()) {
  1160. for (auto const& lib : runtimeEntries->second) {
  1161. if (lib.Target) {
  1162. handleSystemIncludesDep(this->LocalGenerator, lib.Target, config,
  1163. this, &dagChecker, result, excludeImported,
  1164. language);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. std::for_each(result.begin(), result.end(),
  1170. cmSystemTools::ConvertToUnixSlashes);
  1171. std::sort(result.begin(), result.end());
  1172. result.erase(std::unique(result.begin(), result.end()), result.end());
  1173. iter = this->SystemIncludesCache.emplace(key, result).first;
  1174. }
  1175. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  1176. }
  1177. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  1178. {
  1179. return this->Target->GetPropertyAsBool(prop);
  1180. }
  1181. bool cmGeneratorTarget::MaybeHaveInterfaceProperty(
  1182. std::string const& prop, cmGeneratorExpressionContext* context,
  1183. LinkInterfaceFor interfaceFor) const
  1184. {
  1185. std::string const key = prop + '@' + context->Config;
  1186. auto i = this->MaybeInterfacePropertyExists.find(key);
  1187. if (i == this->MaybeInterfacePropertyExists.end()) {
  1188. // Insert an entry now in case there is a cycle.
  1189. i = this->MaybeInterfacePropertyExists.emplace(key, false).first;
  1190. bool& maybeInterfaceProp = i->second;
  1191. // If this target itself has a non-empty property value, we are done.
  1192. maybeInterfaceProp = cmNonempty(this->GetProperty(prop));
  1193. // Otherwise, recurse to interface dependencies.
  1194. if (!maybeInterfaceProp) {
  1195. cmGeneratorTarget const* headTarget =
  1196. context->HeadTarget ? context->HeadTarget : this;
  1197. if (cmLinkInterfaceLibraries const* iface =
  1198. this->GetLinkInterfaceLibraries(context->Config, headTarget,
  1199. interfaceFor)) {
  1200. if (iface->HadHeadSensitiveCondition) {
  1201. // With a different head target we may get to a library with
  1202. // this interface property.
  1203. maybeInterfaceProp = true;
  1204. } else {
  1205. // The transitive interface libraries do not depend on the
  1206. // head target, so we can follow them.
  1207. for (cmLinkItem const& lib : iface->Libraries) {
  1208. if (lib.Target &&
  1209. lib.Target->MaybeHaveInterfaceProperty(prop, context,
  1210. interfaceFor)) {
  1211. maybeInterfaceProp = true;
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. return i->second;
  1220. }
  1221. std::string cmGeneratorTarget::EvaluateInterfaceProperty(
  1222. std::string const& prop, cmGeneratorExpressionContext* context,
  1223. cmGeneratorExpressionDAGChecker* dagCheckerParent,
  1224. LinkInterfaceFor interfaceFor) const
  1225. {
  1226. std::string result;
  1227. // If the property does not appear transitively at all, we are done.
  1228. if (!this->MaybeHaveInterfaceProperty(prop, context, interfaceFor)) {
  1229. return result;
  1230. }
  1231. // Evaluate $<TARGET_PROPERTY:this,prop> as if it were compiled. This is
  1232. // a subset of TargetPropertyNode::Evaluate without stringify/parse steps
  1233. // but sufficient for transitive interface properties.
  1234. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace, this, prop,
  1235. nullptr, dagCheckerParent);
  1236. switch (dagChecker.Check()) {
  1237. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1238. dagChecker.ReportError(
  1239. context, "$<TARGET_PROPERTY:" + this->GetName() + "," + prop + ">");
  1240. return result;
  1241. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1242. // No error. We just skip cyclic references.
  1243. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1244. // No error. We have already seen this transitive property.
  1245. return result;
  1246. case cmGeneratorExpressionDAGChecker::DAG:
  1247. break;
  1248. }
  1249. cmGeneratorTarget const* headTarget =
  1250. context->HeadTarget ? context->HeadTarget : this;
  1251. if (cmValue p = this->GetProperty(prop)) {
  1252. result = cmGeneratorExpressionNode::EvaluateDependentExpression(
  1253. *p, context->LG, context, headTarget, &dagChecker, this);
  1254. }
  1255. if (cmLinkInterfaceLibraries const* iface = this->GetLinkInterfaceLibraries(
  1256. context->Config, headTarget, interfaceFor)) {
  1257. context->HadContextSensitiveCondition =
  1258. context->HadContextSensitiveCondition ||
  1259. iface->HadContextSensitiveCondition;
  1260. for (cmLinkItem const& lib : iface->Libraries) {
  1261. // Broken code can have a target in its own link interface.
  1262. // Don't follow such link interface entries so as not to create a
  1263. // self-referencing loop.
  1264. if (lib.Target && lib.Target != this) {
  1265. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in the
  1266. // above property and hand-evaluate it as if it were compiled.
  1267. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1268. cmGeneratorExpressionContext libContext(
  1269. context->LG, context->Config, context->Quiet, headTarget, this,
  1270. context->EvaluateForBuildsystem, context->Backtrace,
  1271. context->Language);
  1272. std::string libResult = cmGeneratorExpression::StripEmptyListElements(
  1273. lib.Target->EvaluateInterfaceProperty(prop, &libContext, &dagChecker,
  1274. interfaceFor));
  1275. if (!libResult.empty()) {
  1276. if (result.empty()) {
  1277. result = std::move(libResult);
  1278. } else {
  1279. result.reserve(result.size() + 1 + libResult.size());
  1280. result += ";";
  1281. result += libResult;
  1282. }
  1283. }
  1284. context->HadContextSensitiveCondition =
  1285. context->HadContextSensitiveCondition ||
  1286. libContext.HadContextSensitiveCondition;
  1287. context->HadHeadSensitiveCondition =
  1288. context->HadHeadSensitiveCondition ||
  1289. libContext.HadHeadSensitiveCondition;
  1290. }
  1291. }
  1292. }
  1293. return result;
  1294. }
  1295. namespace {
  1296. enum class IncludeDirectoryFallBack
  1297. {
  1298. BINARY,
  1299. OBJECT
  1300. };
  1301. std::string AddLangSpecificInterfaceIncludeDirectories(
  1302. const cmGeneratorTarget* root, const cmGeneratorTarget* target,
  1303. const std::string& lang, const std::string& config,
  1304. const std::string& propertyName, IncludeDirectoryFallBack mode,
  1305. cmGeneratorExpressionDAGChecker* context)
  1306. {
  1307. cmGeneratorExpressionDAGChecker dag{ target->GetBacktrace(), target,
  1308. propertyName, nullptr, context };
  1309. switch (dag.Check()) {
  1310. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1311. dag.ReportError(
  1312. nullptr, "$<TARGET_PROPERTY:" + target->GetName() + ",propertyName");
  1313. CM_FALLTHROUGH;
  1314. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1315. // No error. We just skip cyclic references.
  1316. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1317. // No error. We have already seen this transitive property.
  1318. return "";
  1319. case cmGeneratorExpressionDAGChecker::DAG:
  1320. break;
  1321. }
  1322. std::string directories;
  1323. if (const auto* link_interface = target->GetLinkInterfaceLibraries(
  1324. config, root, LinkInterfaceFor::Usage)) {
  1325. for (const cmLinkItem& library : link_interface->Libraries) {
  1326. if (const cmGeneratorTarget* dependency = library.Target) {
  1327. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  1328. auto* lg = dependency->GetLocalGenerator();
  1329. std::string value = dependency->GetSafeProperty(propertyName);
  1330. if (value.empty()) {
  1331. if (mode == IncludeDirectoryFallBack::BINARY) {
  1332. value = lg->GetCurrentBinaryDirectory();
  1333. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  1334. value = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  1335. lg->GetTargetDirectory(dependency));
  1336. }
  1337. }
  1338. if (!directories.empty()) {
  1339. directories += ";";
  1340. }
  1341. directories += value;
  1342. }
  1343. }
  1344. }
  1345. }
  1346. return directories;
  1347. }
  1348. void AddLangSpecificImplicitIncludeDirectories(
  1349. const cmGeneratorTarget* target, const std::string& lang,
  1350. const std::string& config, const std::string& propertyName,
  1351. IncludeDirectoryFallBack mode, EvaluatedTargetPropertyEntries& entries)
  1352. {
  1353. if (const auto* libraries = target->GetLinkImplementationLibraries(
  1354. config, LinkInterfaceFor::Usage)) {
  1355. cmGeneratorExpressionDAGChecker dag{ target->GetBacktrace(), target,
  1356. propertyName, nullptr, nullptr };
  1357. for (const cmLinkImplItem& library : libraries->Libraries) {
  1358. if (const cmGeneratorTarget* dependency = library.Target) {
  1359. if (!dependency->IsInBuildSystem()) {
  1360. continue;
  1361. }
  1362. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  1363. auto* lg = dependency->GetLocalGenerator();
  1364. EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
  1365. if (cmValue val = dependency->GetProperty(propertyName)) {
  1366. entry.Values.emplace_back(*val);
  1367. } else {
  1368. if (mode == IncludeDirectoryFallBack::BINARY) {
  1369. entry.Values.emplace_back(lg->GetCurrentBinaryDirectory());
  1370. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  1371. entry.Values.emplace_back(
  1372. dependency->GetObjectDirectory(config));
  1373. }
  1374. }
  1375. cmExpandList(
  1376. AddLangSpecificInterfaceIncludeDirectories(
  1377. target, dependency, lang, config, propertyName, mode, &dag),
  1378. entry.Values);
  1379. entries.Entries.emplace_back(std::move(entry));
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. void AddObjectEntries(cmGeneratorTarget const* headTarget,
  1386. std::string const& config,
  1387. cmGeneratorExpressionDAGChecker* dagChecker,
  1388. EvaluatedTargetPropertyEntries& entries)
  1389. {
  1390. if (cmLinkImplementationLibraries const* impl =
  1391. headTarget->GetLinkImplementationLibraries(config,
  1392. LinkInterfaceFor::Usage)) {
  1393. entries.HadContextSensitiveCondition = impl->HadContextSensitiveCondition;
  1394. for (cmLinkImplItem const& lib : impl->Libraries) {
  1395. if (lib.Target &&
  1396. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1397. std::string uniqueName =
  1398. headTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  1399. lib.Target);
  1400. std::string genex = "$<TARGET_OBJECTS:" + std::move(uniqueName) + ">";
  1401. cmGeneratorExpression ge(*headTarget->Makefile->GetCMakeInstance(),
  1402. lib.Backtrace);
  1403. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  1404. cge->SetEvaluateForBuildsystem(true);
  1405. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  1406. cmExpandList(cge->Evaluate(headTarget->GetLocalGenerator(), config,
  1407. headTarget, dagChecker),
  1408. ee.Values);
  1409. if (cge->GetHadContextSensitiveCondition()) {
  1410. ee.ContextDependent = true;
  1411. }
  1412. entries.Entries.emplace_back(std::move(ee));
  1413. }
  1414. }
  1415. }
  1416. }
  1417. void addFileSetEntry(cmGeneratorTarget const* headTarget,
  1418. std::string const& config,
  1419. cmGeneratorExpressionDAGChecker* dagChecker,
  1420. cmFileSet const* fileSet,
  1421. EvaluatedTargetPropertyEntries& entries)
  1422. {
  1423. auto dirCges = fileSet->CompileDirectoryEntries();
  1424. auto dirs = fileSet->EvaluateDirectoryEntries(
  1425. dirCges, headTarget->GetLocalGenerator(), config, headTarget, dagChecker);
  1426. bool contextSensitiveDirs = false;
  1427. for (auto const& dirCge : dirCges) {
  1428. if (dirCge->GetHadContextSensitiveCondition()) {
  1429. contextSensitiveDirs = true;
  1430. break;
  1431. }
  1432. }
  1433. cmake* cm = headTarget->GetLocalGenerator()->GetCMakeInstance();
  1434. for (auto& entryCge : fileSet->CompileFileEntries()) {
  1435. TargetPropertyEntryFileSet tpe(dirs, contextSensitiveDirs,
  1436. std::move(entryCge), fileSet);
  1437. entries.Entries.emplace_back(
  1438. EvaluateTargetPropertyEntry(headTarget, config, "", dagChecker, tpe));
  1439. EvaluatedTargetPropertyEntry const& entry = entries.Entries.back();
  1440. for (auto const& file : entry.Values) {
  1441. auto* sf = headTarget->Makefile->GetOrCreateSource(file);
  1442. if (fileSet->GetType() == "HEADERS"_s) {
  1443. sf->SetProperty("HEADER_FILE_ONLY", "TRUE");
  1444. }
  1445. #ifndef CMAKE_BOOTSTRAP
  1446. std::string e;
  1447. std::string w;
  1448. auto path = sf->ResolveFullPath(&e, &w);
  1449. if (!w.empty()) {
  1450. cm->IssueMessage(MessageType::AUTHOR_WARNING, w, entry.Backtrace);
  1451. }
  1452. if (path.empty()) {
  1453. if (!e.empty()) {
  1454. cm->IssueMessage(MessageType::FATAL_ERROR, e, entry.Backtrace);
  1455. }
  1456. return;
  1457. }
  1458. bool found = false;
  1459. for (auto const& sg : headTarget->Makefile->GetSourceGroups()) {
  1460. if (sg.MatchChildrenFiles(path)) {
  1461. found = true;
  1462. break;
  1463. }
  1464. }
  1465. if (!found) {
  1466. if (fileSet->GetType() == "HEADERS"_s) {
  1467. headTarget->Makefile->GetOrCreateSourceGroup("Header Files")
  1468. ->AddGroupFile(path);
  1469. }
  1470. }
  1471. #endif
  1472. }
  1473. }
  1474. }
  1475. void AddFileSetEntries(cmGeneratorTarget const* headTarget,
  1476. std::string const& config,
  1477. cmGeneratorExpressionDAGChecker* dagChecker,
  1478. EvaluatedTargetPropertyEntries& entries)
  1479. {
  1480. for (auto const& entry : headTarget->Target->GetHeaderSetsEntries()) {
  1481. for (auto const& name : cmList{ entry.Value }) {
  1482. auto const* headerSet = headTarget->Target->GetFileSet(name);
  1483. addFileSetEntry(headTarget, config, dagChecker, headerSet, entries);
  1484. }
  1485. }
  1486. for (auto const& entry : headTarget->Target->GetCxxModuleSetsEntries()) {
  1487. for (auto const& name : cmList{ entry.Value }) {
  1488. auto const* cxxModuleSet = headTarget->Target->GetFileSet(name);
  1489. addFileSetEntry(headTarget, config, dagChecker, cxxModuleSet, entries);
  1490. }
  1491. }
  1492. }
  1493. bool processSources(cmGeneratorTarget const* tgt,
  1494. EvaluatedTargetPropertyEntries& entries,
  1495. std::vector<BT<std::string>>& srcs,
  1496. std::unordered_set<std::string>& uniqueSrcs,
  1497. bool debugSources)
  1498. {
  1499. cmMakefile* mf = tgt->Target->GetMakefile();
  1500. bool contextDependent = entries.HadContextSensitiveCondition;
  1501. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  1502. if (entry.ContextDependent) {
  1503. contextDependent = true;
  1504. }
  1505. cmLinkImplItem const& item = entry.LinkImplItem;
  1506. std::string const& targetName = item.AsStr();
  1507. for (std::string& src : entry.Values) {
  1508. cmSourceFile* sf = mf->GetOrCreateSource(src);
  1509. std::string e;
  1510. std::string w;
  1511. std::string fullPath = sf->ResolveFullPath(&e, &w);
  1512. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  1513. if (!w.empty()) {
  1514. cm->IssueMessage(MessageType::AUTHOR_WARNING, w, entry.Backtrace);
  1515. }
  1516. if (fullPath.empty()) {
  1517. if (!e.empty()) {
  1518. cm->IssueMessage(MessageType::FATAL_ERROR, e, entry.Backtrace);
  1519. }
  1520. return contextDependent;
  1521. }
  1522. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  1523. std::ostringstream err;
  1524. if (!targetName.empty()) {
  1525. err << "Target \"" << targetName
  1526. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  1527. << src << "\"";
  1528. } else {
  1529. err << "Found relative path while evaluating sources of \""
  1530. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  1531. }
  1532. tgt->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  1533. err.str());
  1534. return contextDependent;
  1535. }
  1536. src = fullPath;
  1537. }
  1538. std::string usedSources;
  1539. for (std::string const& src : entry.Values) {
  1540. if (uniqueSrcs.insert(src).second) {
  1541. srcs.emplace_back(src, entry.Backtrace);
  1542. if (debugSources) {
  1543. usedSources += " * " + src + "\n";
  1544. }
  1545. }
  1546. }
  1547. if (!usedSources.empty()) {
  1548. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1549. MessageType::LOG,
  1550. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  1551. usedSources,
  1552. entry.Backtrace);
  1553. }
  1554. }
  1555. return contextDependent;
  1556. }
  1557. }
  1558. std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths(
  1559. std::string const& config) const
  1560. {
  1561. std::vector<BT<std::string>> files;
  1562. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1563. // At configure-time, this method can be called as part of getting the
  1564. // LOCATION property or to export() a file to be include()d. However
  1565. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  1566. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  1567. // behavior of CMP0024 and CMP0026 only.
  1568. cmBTStringRange sourceEntries = this->Target->GetSourceEntries();
  1569. for (auto const& entry : sourceEntries) {
  1570. cmList items{ entry.Value };
  1571. for (auto const& item : items) {
  1572. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  1573. item.back() == '>') {
  1574. continue;
  1575. }
  1576. files.emplace_back(item);
  1577. }
  1578. }
  1579. return files;
  1580. }
  1581. cmList debugProperties{ this->Makefile->GetDefinition(
  1582. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  1583. bool debugSources =
  1584. !this->DebugSourcesDone && cm::contains(debugProperties, "SOURCES");
  1585. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1586. this->DebugSourcesDone = true;
  1587. }
  1588. cmGeneratorExpressionDAGChecker dagChecker(this, "SOURCES", nullptr,
  1589. nullptr);
  1590. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  1591. this, config, std::string(), &dagChecker, this->SourceEntries);
  1592. std::unordered_set<std::string> uniqueSrcs;
  1593. bool contextDependentDirectSources =
  1594. processSources(this, entries, files, uniqueSrcs, debugSources);
  1595. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  1596. EvaluatedTargetPropertyEntries linkInterfaceSourcesEntries;
  1597. AddInterfaceEntries(this, config, "INTERFACE_SOURCES", std::string(),
  1598. &dagChecker, linkInterfaceSourcesEntries,
  1599. IncludeRuntimeInterface::No, LinkInterfaceFor::Usage);
  1600. std::vector<std::string>::size_type numFilesBefore = files.size();
  1601. bool contextDependentInterfaceSources = processSources(
  1602. this, linkInterfaceSourcesEntries, files, uniqueSrcs, debugSources);
  1603. // Collect TARGET_OBJECTS of direct object link-dependencies.
  1604. bool contextDependentObjects = false;
  1605. std::vector<std::string>::size_type numFilesBefore2 = files.size();
  1606. if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1607. EvaluatedTargetPropertyEntries linkObjectsEntries;
  1608. AddObjectEntries(this, config, &dagChecker, linkObjectsEntries);
  1609. contextDependentObjects = processSources(this, linkObjectsEntries, files,
  1610. uniqueSrcs, debugSources);
  1611. }
  1612. // Collect this target's file sets.
  1613. std::vector<std::string>::size_type numFilesBefore3 = files.size();
  1614. EvaluatedTargetPropertyEntries fileSetEntries;
  1615. AddFileSetEntries(this, config, &dagChecker, fileSetEntries);
  1616. bool contextDependentFileSets =
  1617. processSources(this, fileSetEntries, files, uniqueSrcs, debugSources);
  1618. // Determine if sources are context-dependent or not.
  1619. if (!contextDependentDirectSources &&
  1620. !(contextDependentInterfaceSources && numFilesBefore < files.size()) &&
  1621. !(contextDependentObjects && numFilesBefore2 < files.size()) &&
  1622. !(contextDependentFileSets && numFilesBefore3 < files.size())) {
  1623. this->SourcesAreContextDependent = Tribool::False;
  1624. } else {
  1625. this->SourcesAreContextDependent = Tribool::True;
  1626. }
  1627. return files;
  1628. }
  1629. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  1630. const std::string& config) const
  1631. {
  1632. std::vector<BT<cmSourceFile*>> tmp = this->GetSourceFiles(config);
  1633. files.reserve(tmp.size());
  1634. for (BT<cmSourceFile*>& v : tmp) {
  1635. files.push_back(v.Value);
  1636. }
  1637. }
  1638. std::vector<BT<cmSourceFile*>> cmGeneratorTarget::GetSourceFiles(
  1639. std::string const& config) const
  1640. {
  1641. std::vector<BT<cmSourceFile*>> files;
  1642. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  1643. // Since we are still configuring not all sources may exist yet,
  1644. // so we need to avoid full source classification because that
  1645. // requires the absolute paths to all sources to be determined.
  1646. // Since this is only for compatibility with old policies that
  1647. // projects should not depend on anymore, just compute the files
  1648. // without memoizing them.
  1649. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1650. std::set<cmSourceFile*> emitted;
  1651. for (BT<std::string> const& s : srcs) {
  1652. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1653. if (emitted.insert(sf).second) {
  1654. files.emplace_back(sf, s.Backtrace);
  1655. }
  1656. }
  1657. return files;
  1658. }
  1659. KindedSources const& kinded = this->GetKindedSources(config);
  1660. files.reserve(kinded.Sources.size());
  1661. for (SourceAndKind const& si : kinded.Sources) {
  1662. files.push_back(si.Source);
  1663. }
  1664. return files;
  1665. }
  1666. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1667. std::vector<cmSourceFile*>& files, const std::string& config) const
  1668. {
  1669. std::vector<BT<cmSourceFile*>> tmp =
  1670. this->GetSourceFilesWithoutObjectLibraries(config);
  1671. files.reserve(tmp.size());
  1672. for (BT<cmSourceFile*>& v : tmp) {
  1673. files.push_back(v.Value);
  1674. }
  1675. }
  1676. std::vector<BT<cmSourceFile*>>
  1677. cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1678. std::string const& config) const
  1679. {
  1680. std::vector<BT<cmSourceFile*>> files;
  1681. KindedSources const& kinded = this->GetKindedSources(config);
  1682. files.reserve(kinded.Sources.size());
  1683. for (SourceAndKind const& si : kinded.Sources) {
  1684. if (si.Source.Value->GetObjectLibrary().empty()) {
  1685. files.push_back(si.Source);
  1686. }
  1687. }
  1688. return files;
  1689. }
  1690. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  1691. std::string const& config) const
  1692. {
  1693. // If we already processed one configuration and found no dependency
  1694. // on configuration then always use the one result.
  1695. if (this->SourcesAreContextDependent == Tribool::False) {
  1696. return this->KindedSourcesMap.begin()->second;
  1697. }
  1698. // Lookup any existing link implementation for this configuration.
  1699. std::string const key = cmSystemTools::UpperCase(config);
  1700. auto it = this->KindedSourcesMap.find(key);
  1701. if (it != this->KindedSourcesMap.end()) {
  1702. if (!it->second.Initialized) {
  1703. std::ostringstream e;
  1704. e << "The SOURCES of \"" << this->GetName()
  1705. << "\" use a generator expression that depends on the "
  1706. "SOURCES themselves.";
  1707. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1708. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1709. static KindedSources empty;
  1710. return empty;
  1711. }
  1712. return it->second;
  1713. }
  1714. // Add an entry to the map for this configuration.
  1715. KindedSources& files = this->KindedSourcesMap[key];
  1716. this->ComputeKindedSources(files, config);
  1717. files.Initialized = true;
  1718. return files;
  1719. }
  1720. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  1721. std::string const& config) const
  1722. {
  1723. // Get the source file paths by string.
  1724. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1725. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  1726. std::vector<cmSourceFile*> badObjLib;
  1727. std::set<cmSourceFile*> emitted;
  1728. for (BT<std::string> const& s : srcs) {
  1729. // Create each source at most once.
  1730. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1731. if (!emitted.insert(sf).second) {
  1732. continue;
  1733. }
  1734. // Compute the kind (classification) of this source file.
  1735. SourceKind kind;
  1736. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  1737. cmFileSet const* fs = this->GetFileSetForSource(config, sf);
  1738. if (sf->GetCustomCommand()) {
  1739. kind = SourceKindCustomCommand;
  1740. } else if (!this->Target->IsNormal() && !this->Target->IsImported() &&
  1741. fs && (fs->GetType() == "CXX_MODULES"_s)) {
  1742. kind = SourceKindCxxModuleSource;
  1743. } else if (this->Target->GetType() == cmStateEnums::UTILITY ||
  1744. this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY
  1745. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1746. // NOLINTNEXTLINE(bugprone-branch-clone)
  1747. ) {
  1748. kind = SourceKindExtra;
  1749. } else if (this->IsSourceFilePartOfUnityBatch(sf->ResolveFullPath())) {
  1750. kind = SourceKindUnityBatched;
  1751. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1752. // NOLINTNEXTLINE(bugprone-branch-clone)
  1753. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  1754. kind = SourceKindHeader;
  1755. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1756. kind = SourceKindExternalObject;
  1757. } else if (!sf->GetOrDetermineLanguage().empty()) {
  1758. kind = SourceKindObjectSource;
  1759. } else if (ext == "def") {
  1760. kind = SourceKindModuleDefinition;
  1761. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1762. badObjLib.push_back(sf);
  1763. }
  1764. } else if (ext == "idl") {
  1765. kind = SourceKindIDL;
  1766. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1767. badObjLib.push_back(sf);
  1768. }
  1769. } else if (ext == "resx") {
  1770. kind = SourceKindResx;
  1771. } else if (ext == "appxmanifest") {
  1772. kind = SourceKindAppManifest;
  1773. } else if (ext == "manifest") {
  1774. if (sf->GetPropertyAsBool("VS_DEPLOYMENT_CONTENT")) {
  1775. kind = SourceKindExtra;
  1776. } else {
  1777. kind = SourceKindManifest;
  1778. }
  1779. } else if (ext == "pfx") {
  1780. kind = SourceKindCertificate;
  1781. } else if (ext == "xaml") {
  1782. kind = SourceKindXaml;
  1783. } else if (header_regex.find(sf->ResolveFullPath())) {
  1784. kind = SourceKindHeader;
  1785. } else {
  1786. kind = SourceKindExtra;
  1787. }
  1788. // Save this classified source file in the result vector.
  1789. files.Sources.push_back({ BT<cmSourceFile*>(sf, s.Backtrace), kind });
  1790. }
  1791. if (!badObjLib.empty()) {
  1792. std::ostringstream e;
  1793. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  1794. for (cmSourceFile* i : badObjLib) {
  1795. e << " " << i->GetLocation().GetName() << "\n";
  1796. }
  1797. e << "but may contain only sources that compile, header files, and "
  1798. "other files that would not affect linking of a normal library.";
  1799. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1800. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1801. }
  1802. }
  1803. std::vector<cmGeneratorTarget::AllConfigSource> const&
  1804. cmGeneratorTarget::GetAllConfigSources() const
  1805. {
  1806. if (this->AllConfigSources.empty()) {
  1807. this->ComputeAllConfigSources();
  1808. }
  1809. return this->AllConfigSources;
  1810. }
  1811. void cmGeneratorTarget::ComputeAllConfigSources() const
  1812. {
  1813. std::vector<std::string> configs =
  1814. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  1815. std::map<cmSourceFile const*, size_t> index;
  1816. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1817. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1818. for (SourceAndKind const& src : sources.Sources) {
  1819. auto mi = index.find(src.Source.Value);
  1820. if (mi == index.end()) {
  1821. AllConfigSource acs;
  1822. acs.Source = src.Source.Value;
  1823. acs.Kind = src.Kind;
  1824. this->AllConfigSources.push_back(std::move(acs));
  1825. std::map<cmSourceFile const*, size_t>::value_type entry(
  1826. src.Source.Value, this->AllConfigSources.size() - 1);
  1827. mi = index.insert(entry).first;
  1828. }
  1829. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1830. }
  1831. }
  1832. }
  1833. std::vector<cmGeneratorTarget::AllConfigSource>
  1834. cmGeneratorTarget::GetAllConfigSources(SourceKind kind) const
  1835. {
  1836. std::vector<AllConfigSource> result;
  1837. for (AllConfigSource const& source : this->GetAllConfigSources()) {
  1838. if (source.Kind == kind) {
  1839. result.push_back(source);
  1840. }
  1841. }
  1842. return result;
  1843. }
  1844. std::set<std::string> cmGeneratorTarget::GetAllConfigCompileLanguages() const
  1845. {
  1846. std::set<std::string> languages;
  1847. std::vector<AllConfigSource> const& sources = this->GetAllConfigSources();
  1848. for (AllConfigSource const& si : sources) {
  1849. std::string const& lang = si.Source->GetOrDetermineLanguage();
  1850. if (!lang.empty()) {
  1851. languages.emplace(lang);
  1852. }
  1853. }
  1854. return languages;
  1855. }
  1856. std::string cmGeneratorTarget::GetCompilePDBName(
  1857. const std::string& config) const
  1858. {
  1859. // Check for a per-configuration output directory target property.
  1860. std::string configUpper = cmSystemTools::UpperCase(config);
  1861. std::string configProp = cmStrCat("COMPILE_PDB_NAME_", configUpper);
  1862. cmValue config_name = this->GetProperty(configProp);
  1863. if (cmNonempty(config_name)) {
  1864. NameComponents const& components = GetFullNameInternalComponents(
  1865. config, cmStateEnums::RuntimeBinaryArtifact);
  1866. return components.prefix + *config_name + ".pdb";
  1867. }
  1868. cmValue name = this->GetProperty("COMPILE_PDB_NAME");
  1869. if (cmNonempty(name)) {
  1870. NameComponents const& components = GetFullNameInternalComponents(
  1871. config, cmStateEnums::RuntimeBinaryArtifact);
  1872. return components.prefix + *name + ".pdb";
  1873. }
  1874. return "";
  1875. }
  1876. std::string cmGeneratorTarget::GetCompilePDBPath(
  1877. const std::string& config) const
  1878. {
  1879. std::string dir = this->GetCompilePDBDirectory(config);
  1880. std::string name = this->GetCompilePDBName(config);
  1881. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1882. dir = this->GetPDBDirectory(config);
  1883. }
  1884. if (!dir.empty()) {
  1885. dir += "/";
  1886. }
  1887. return dir + name;
  1888. }
  1889. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1890. {
  1891. // soname is supported only for shared libraries and modules,
  1892. // and then only when the platform supports an soname flag.
  1893. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1894. !this->GetPropertyAsBool("NO_SONAME") &&
  1895. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  1896. }
  1897. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  1898. const std::string& config) const
  1899. {
  1900. // Only executables and shared libraries can have an rpath and may
  1901. // need relinking.
  1902. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  1903. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1904. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1905. return false;
  1906. }
  1907. // If there is no install location this target will not be installed
  1908. // and therefore does not need relinking.
  1909. if (!this->Target->GetHaveInstallRule()) {
  1910. return false;
  1911. }
  1912. // If skipping all rpaths completely then no relinking is needed.
  1913. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1914. return false;
  1915. }
  1916. // If building with the install-tree rpath no relinking is needed.
  1917. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1918. return false;
  1919. }
  1920. // If chrpath is going to be used no relinking is needed.
  1921. if (this->IsChrpathUsed(config)) {
  1922. return false;
  1923. }
  1924. // Check for rpath support on this platform.
  1925. std::string ll = this->GetLinkerLanguage(config);
  1926. if (!ll.empty()) {
  1927. std::string flagVar =
  1928. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG");
  1929. if (!this->Makefile->IsSet(flagVar)) {
  1930. // There is no rpath support on this platform so nothing needs
  1931. // relinking.
  1932. return false;
  1933. }
  1934. } else {
  1935. // No linker language is known. This error will be reported by
  1936. // other code.
  1937. return false;
  1938. }
  1939. // If either a build or install tree rpath is set then the rpath
  1940. // will likely change between the build tree and install tree and
  1941. // this target must be relinked.
  1942. bool have_rpath =
  1943. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH(config);
  1944. bool is_ninja = this->LocalGenerator->GetGlobalGenerator()->IsNinja();
  1945. if (have_rpath && is_ninja) {
  1946. std::ostringstream w;
  1947. /* clang-format off */
  1948. w <<
  1949. "The install of the " << this->GetName() << " target requires changing "
  1950. "an RPATH from the build tree, but this is not supported with the Ninja "
  1951. "generator unless on an ELF-based or XCOFF-based platform. "
  1952. "The CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  1953. "relinking step."
  1954. ;
  1955. /* clang-format on */
  1956. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1957. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  1958. }
  1959. return have_rpath;
  1960. }
  1961. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  1962. {
  1963. // Only certain target types have an rpath.
  1964. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1965. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1966. this->GetType() == cmStateEnums::EXECUTABLE)) {
  1967. return false;
  1968. }
  1969. // If the target will not be installed we do not need to change its
  1970. // rpath.
  1971. if (!this->Target->GetHaveInstallRule()) {
  1972. return false;
  1973. }
  1974. // Skip chrpath if skipping rpath altogether.
  1975. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1976. return false;
  1977. }
  1978. // Skip chrpath if it does not need to be changed at install time.
  1979. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1980. return false;
  1981. }
  1982. // Allow the user to disable builtin chrpath explicitly.
  1983. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  1984. return false;
  1985. }
  1986. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1987. return true;
  1988. }
  1989. // Enable if the rpath flag uses a separator and the target uses
  1990. // binaries we know how to edit.
  1991. std::string ll = this->GetLinkerLanguage(config);
  1992. if (!ll.empty()) {
  1993. std::string sepVar =
  1994. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG_SEP");
  1995. cmValue sep = this->Makefile->GetDefinition(sepVar);
  1996. if (cmNonempty(sep)) {
  1997. // TODO: Add binary format check to ABI detection and get rid of
  1998. // CMAKE_EXECUTABLE_FORMAT.
  1999. if (cmValue fmt =
  2000. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  2001. if (*fmt == "ELF") {
  2002. return true;
  2003. }
  2004. #if defined(CMake_USE_XCOFF_PARSER)
  2005. if (*fmt == "XCOFF") {
  2006. return true;
  2007. }
  2008. #endif
  2009. }
  2010. }
  2011. }
  2012. return false;
  2013. }
  2014. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  2015. const std::string& config) const
  2016. {
  2017. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2018. if (cmGeneratorTarget::ImportInfo const* info =
  2019. this->GetImportInfo(config)) {
  2020. return info->NoSOName;
  2021. }
  2022. }
  2023. return false;
  2024. }
  2025. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  2026. const std::string& config) const
  2027. {
  2028. TargetPtrToBoolMap& cache = this->MacOSXRpathInstallNameDirCache[config];
  2029. const auto lookup = cache.find(this->Target);
  2030. if (lookup != cache.cend()) {
  2031. return lookup->second;
  2032. }
  2033. const bool result = this->DetermineHasMacOSXRpathInstallNameDir(config);
  2034. cache[this->Target] = result;
  2035. return result;
  2036. }
  2037. bool cmGeneratorTarget::DetermineHasMacOSXRpathInstallNameDir(
  2038. const std::string& config) const
  2039. {
  2040. bool install_name_is_rpath = false;
  2041. bool macosx_rpath = false;
  2042. if (!this->IsImported()) {
  2043. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2044. return false;
  2045. }
  2046. cmValue install_name = this->GetProperty("INSTALL_NAME_DIR");
  2047. bool use_install_name = this->MacOSXUseInstallNameDir();
  2048. if (install_name && use_install_name && *install_name == "@rpath") {
  2049. install_name_is_rpath = true;
  2050. } else if (install_name && use_install_name) {
  2051. return false;
  2052. }
  2053. if (!install_name_is_rpath) {
  2054. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  2055. }
  2056. } else {
  2057. // Lookup the imported soname.
  2058. if (cmGeneratorTarget::ImportInfo const* info =
  2059. this->GetImportInfo(config)) {
  2060. if (!info->NoSOName && !info->SOName.empty()) {
  2061. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  2062. install_name_is_rpath = true;
  2063. }
  2064. } else {
  2065. std::string install_name;
  2066. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  2067. if (install_name.find("@rpath") != std::string::npos) {
  2068. install_name_is_rpath = true;
  2069. }
  2070. }
  2071. }
  2072. }
  2073. if (!install_name_is_rpath && !macosx_rpath) {
  2074. return false;
  2075. }
  2076. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  2077. std::ostringstream w;
  2078. w << "Attempting to use ";
  2079. if (macosx_rpath) {
  2080. w << "MACOSX_RPATH";
  2081. } else {
  2082. w << "@rpath";
  2083. }
  2084. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  2085. w << " This could be because you are using a Mac OS X version";
  2086. w << " less than 10.5 or because CMake's platform configuration is";
  2087. w << " corrupt.";
  2088. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  2089. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  2090. }
  2091. return true;
  2092. }
  2093. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  2094. {
  2095. // we can't do rpaths when unsupported
  2096. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  2097. return false;
  2098. }
  2099. cmValue macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  2100. if (macosx_rpath_str) {
  2101. return this->GetPropertyAsBool("MACOSX_RPATH");
  2102. }
  2103. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  2104. if (cmp0042 == cmPolicies::WARN) {
  2105. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  2106. this->GetName());
  2107. }
  2108. return cmp0042 == cmPolicies::NEW;
  2109. }
  2110. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  2111. {
  2112. cmValue build_with_install_name =
  2113. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  2114. if (build_with_install_name) {
  2115. return cmIsOn(*build_with_install_name);
  2116. }
  2117. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  2118. if (cmp0068 == cmPolicies::NEW) {
  2119. return false;
  2120. }
  2121. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  2122. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  2123. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  2124. this->GetName());
  2125. }
  2126. return use_install_name;
  2127. }
  2128. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  2129. InstallNameType name_type) const
  2130. {
  2131. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  2132. if (cmp0068 == cmPolicies::NEW) {
  2133. return true;
  2134. }
  2135. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  2136. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  2137. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  2138. } else {
  2139. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  2140. }
  2141. if (skip && cmp0068 == cmPolicies::WARN) {
  2142. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  2143. this->GetName());
  2144. }
  2145. return !skip;
  2146. }
  2147. std::string cmGeneratorTarget::GetSOName(
  2148. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2149. {
  2150. if (this->IsImported()) {
  2151. // Lookup the imported soname.
  2152. if (cmGeneratorTarget::ImportInfo const* info =
  2153. this->GetImportInfo(config)) {
  2154. if (info->NoSOName) {
  2155. // The imported library has no builtin soname so the name
  2156. // searched at runtime will be just the filename.
  2157. return cmSystemTools::GetFilenameName(info->Location);
  2158. }
  2159. // Use the soname given if any.
  2160. if (this->IsFrameworkOnApple()) {
  2161. auto fwDescriptor = this->GetGlobalGenerator()->SplitFrameworkPath(
  2162. info->SOName, cmGlobalGenerator::FrameworkFormat::Strict);
  2163. if (fwDescriptor) {
  2164. return fwDescriptor->GetVersionedName();
  2165. }
  2166. }
  2167. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  2168. return info->SOName.substr(cmStrLen("@rpath/"));
  2169. }
  2170. return info->SOName;
  2171. }
  2172. return "";
  2173. }
  2174. // Compute the soname that will be built.
  2175. return artifact == cmStateEnums::RuntimeBinaryArtifact
  2176. ? this->GetLibraryNames(config).SharedObject
  2177. : this->GetLibraryNames(config).ImportLibrary;
  2178. }
  2179. namespace {
  2180. bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  2181. {
  2182. return level == cmGeneratorTarget::FullLevel;
  2183. }
  2184. bool shouldAddContentLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  2185. {
  2186. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  2187. }
  2188. }
  2189. std::string cmGeneratorTarget::GetAppBundleDirectory(
  2190. const std::string& config, BundleDirectoryLevel level) const
  2191. {
  2192. std::string fpath = cmStrCat(
  2193. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2194. cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
  2195. fpath += (ext ? *ext : "app");
  2196. if (shouldAddContentLevel(level) &&
  2197. !this->Makefile->PlatformIsAppleEmbedded()) {
  2198. fpath += "/Contents";
  2199. if (shouldAddFullLevel(level)) {
  2200. fpath += "/MacOS";
  2201. }
  2202. }
  2203. return fpath;
  2204. }
  2205. bool cmGeneratorTarget::IsBundleOnApple() const
  2206. {
  2207. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  2208. this->IsCFBundleOnApple();
  2209. }
  2210. bool cmGeneratorTarget::IsWin32Executable(const std::string& config) const
  2211. {
  2212. return cmIsOn(cmGeneratorExpression::Evaluate(
  2213. this->GetSafeProperty("WIN32_EXECUTABLE"), this->LocalGenerator, config));
  2214. }
  2215. std::string cmGeneratorTarget::GetCFBundleDirectory(
  2216. const std::string& config, BundleDirectoryLevel level) const
  2217. {
  2218. std::string fpath = cmStrCat(
  2219. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2220. std::string ext;
  2221. if (cmValue p = this->GetProperty("BUNDLE_EXTENSION")) {
  2222. ext = *p;
  2223. } else {
  2224. if (this->IsXCTestOnApple()) {
  2225. ext = "xctest";
  2226. } else {
  2227. ext = "bundle";
  2228. }
  2229. }
  2230. fpath += ext;
  2231. if (shouldAddContentLevel(level) &&
  2232. !this->Makefile->PlatformIsAppleEmbedded()) {
  2233. fpath += "/Contents";
  2234. if (shouldAddFullLevel(level)) {
  2235. fpath += "/MacOS";
  2236. }
  2237. }
  2238. return fpath;
  2239. }
  2240. std::string cmGeneratorTarget::GetFrameworkDirectory(
  2241. const std::string& config, BundleDirectoryLevel level) const
  2242. {
  2243. std::string fpath = cmStrCat(
  2244. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  2245. cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
  2246. fpath += (ext ? *ext : "framework");
  2247. if (shouldAddFullLevel(level) &&
  2248. !this->Makefile->PlatformIsAppleEmbedded()) {
  2249. fpath += "/Versions/";
  2250. fpath += this->GetFrameworkVersion();
  2251. }
  2252. return fpath;
  2253. }
  2254. std::string cmGeneratorTarget::GetFullName(
  2255. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2256. {
  2257. if (this->IsImported()) {
  2258. return this->GetFullNameImported(config, artifact);
  2259. }
  2260. return this->GetFullNameInternal(config, artifact);
  2261. }
  2262. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  2263. const std::string& config) const
  2264. {
  2265. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2266. // If building directly for installation then the build tree install_name
  2267. // is the same as the install tree.
  2268. if (this->MacOSXUseInstallNameDir()) {
  2269. std::string installPrefix =
  2270. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  2271. return this->GetInstallNameDirForInstallTree(config, installPrefix);
  2272. }
  2273. // Use the build tree directory for the target.
  2274. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  2275. std::string dir;
  2276. if (this->MacOSXRpathInstallNameDirDefault()) {
  2277. dir = "@rpath";
  2278. } else {
  2279. dir = this->GetDirectory(config);
  2280. }
  2281. dir += "/";
  2282. return dir;
  2283. }
  2284. }
  2285. return "";
  2286. }
  2287. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree(
  2288. const std::string& config, const std::string& installPrefix) const
  2289. {
  2290. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2291. std::string dir;
  2292. cmValue install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  2293. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  2294. if (cmNonempty(install_name_dir)) {
  2295. dir = *install_name_dir;
  2296. cmGeneratorExpression::ReplaceInstallPrefix(dir, installPrefix);
  2297. dir =
  2298. cmGeneratorExpression::Evaluate(dir, this->LocalGenerator, config);
  2299. if (!dir.empty()) {
  2300. dir = cmStrCat(dir, '/');
  2301. }
  2302. }
  2303. }
  2304. if (!install_name_dir) {
  2305. if (this->MacOSXRpathInstallNameDirDefault()) {
  2306. dir = "@rpath/";
  2307. }
  2308. }
  2309. return dir;
  2310. }
  2311. return "";
  2312. }
  2313. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  2314. {
  2315. return this->Target->GetBacktrace();
  2316. }
  2317. const std::set<BT<std::pair<std::string, bool>>>&
  2318. cmGeneratorTarget::GetUtilities() const
  2319. {
  2320. return this->Target->GetUtilities();
  2321. }
  2322. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  2323. {
  2324. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2325. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2326. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2327. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2328. this->GetType() == cmStateEnums::EXECUTABLE;
  2329. }
  2330. const std::string* cmGeneratorTarget::GetExportMacro() const
  2331. {
  2332. // Define the symbol for targets that export symbols.
  2333. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2334. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2335. this->IsExecutableWithExports()) {
  2336. if (cmValue custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  2337. this->ExportMacro = *custom_export_name;
  2338. } else {
  2339. std::string in = cmStrCat(this->GetName(), "_EXPORTS");
  2340. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  2341. }
  2342. return &this->ExportMacro;
  2343. }
  2344. return nullptr;
  2345. }
  2346. class cmTargetCollectLinkLanguages
  2347. {
  2348. public:
  2349. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  2350. std::string config,
  2351. std::unordered_set<std::string>& languages,
  2352. cmGeneratorTarget const* head, bool secondPass)
  2353. : Config(std::move(config))
  2354. , Languages(languages)
  2355. , HeadTarget(head)
  2356. , SecondPass(secondPass)
  2357. {
  2358. this->Visited.insert(target);
  2359. }
  2360. void Visit(cmLinkItem const& item)
  2361. {
  2362. if (!item.Target) {
  2363. return;
  2364. }
  2365. if (!this->Visited.insert(item.Target).second) {
  2366. return;
  2367. }
  2368. cmLinkInterface const* iface = item.Target->GetLinkInterface(
  2369. this->Config, this->HeadTarget, this->SecondPass);
  2370. if (!iface) {
  2371. return;
  2372. }
  2373. if (iface->HadLinkLanguageSensitiveCondition) {
  2374. this->HadLinkLanguageSensitiveCondition = true;
  2375. }
  2376. for (std::string const& language : iface->Languages) {
  2377. this->Languages.insert(language);
  2378. }
  2379. for (cmLinkItem const& lib : iface->Libraries) {
  2380. this->Visit(lib);
  2381. }
  2382. }
  2383. bool GetHadLinkLanguageSensitiveCondition() const
  2384. {
  2385. return this->HadLinkLanguageSensitiveCondition;
  2386. }
  2387. private:
  2388. std::string Config;
  2389. std::unordered_set<std::string>& Languages;
  2390. cmGeneratorTarget const* HeadTarget;
  2391. std::set<cmGeneratorTarget const*> Visited;
  2392. bool SecondPass;
  2393. bool HadLinkLanguageSensitiveCondition = false;
  2394. };
  2395. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  2396. const std::string& config) const
  2397. {
  2398. // There is no link implementation for targets that cannot compile sources.
  2399. if (!this->CanCompileSources()) {
  2400. static LinkClosure const empty = { {}, {} };
  2401. return &empty;
  2402. }
  2403. std::string key(cmSystemTools::UpperCase(config));
  2404. auto i = this->LinkClosureMap.find(key);
  2405. if (i == this->LinkClosureMap.end()) {
  2406. LinkClosure lc;
  2407. this->ComputeLinkClosure(config, lc);
  2408. LinkClosureMapType::value_type entry(key, lc);
  2409. i = this->LinkClosureMap.insert(entry).first;
  2410. }
  2411. return &i->second;
  2412. }
  2413. class cmTargetSelectLinker
  2414. {
  2415. int Preference = 0;
  2416. cmGeneratorTarget const* Target;
  2417. cmGlobalGenerator* GG;
  2418. std::set<std::string> Preferred;
  2419. public:
  2420. cmTargetSelectLinker(cmGeneratorTarget const* target)
  2421. : Target(target)
  2422. {
  2423. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  2424. }
  2425. void Consider(const std::string& lang)
  2426. {
  2427. int preference = this->GG->GetLinkerPreference(lang);
  2428. if (preference > this->Preference) {
  2429. this->Preference = preference;
  2430. this->Preferred.clear();
  2431. }
  2432. if (preference == this->Preference) {
  2433. this->Preferred.insert(lang);
  2434. }
  2435. }
  2436. std::string Choose()
  2437. {
  2438. if (this->Preferred.empty()) {
  2439. return "";
  2440. }
  2441. if (this->Preferred.size() > 1) {
  2442. std::ostringstream e;
  2443. e << "Target " << this->Target->GetName()
  2444. << " contains multiple languages with the highest linker preference"
  2445. << " (" << this->Preference << "):\n";
  2446. for (std::string const& li : this->Preferred) {
  2447. e << " " << li << "\n";
  2448. }
  2449. e << "Set the LINKER_LANGUAGE property for this target.";
  2450. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  2451. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  2452. this->Target->GetBacktrace());
  2453. }
  2454. return *this->Preferred.begin();
  2455. }
  2456. };
  2457. bool cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2458. LinkClosure& lc,
  2459. bool secondPass) const
  2460. {
  2461. // Get languages built in this target.
  2462. std::unordered_set<std::string> languages;
  2463. cmLinkImplementation const* impl =
  2464. this->GetLinkImplementation(config, LinkInterfaceFor::Link, secondPass);
  2465. assert(impl);
  2466. languages.insert(impl->Languages.cbegin(), impl->Languages.cend());
  2467. // Add interface languages from linked targets.
  2468. // cmTargetCollectLinkLanguages cll(this, config, languages, this,
  2469. // secondPass);
  2470. cmTargetCollectLinkLanguages cll(this, config, languages, this, secondPass);
  2471. for (cmLinkImplItem const& lib : impl->Libraries) {
  2472. cll.Visit(lib);
  2473. }
  2474. // Store the transitive closure of languages.
  2475. cm::append(lc.Languages, languages);
  2476. // Choose the language whose linker should be used.
  2477. if (secondPass || lc.LinkerLanguage.empty()) {
  2478. // Find the language with the highest preference value.
  2479. cmTargetSelectLinker tsl(this);
  2480. // First select from the languages compiled directly in this target.
  2481. for (std::string const& l : impl->Languages) {
  2482. tsl.Consider(l);
  2483. }
  2484. // Now consider languages that propagate from linked targets.
  2485. for (std::string const& lang : languages) {
  2486. std::string propagates =
  2487. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  2488. if (this->Makefile->IsOn(propagates)) {
  2489. tsl.Consider(lang);
  2490. }
  2491. }
  2492. lc.LinkerLanguage = tsl.Choose();
  2493. }
  2494. return impl->HadLinkLanguageSensitiveCondition ||
  2495. cll.GetHadLinkLanguageSensitiveCondition();
  2496. }
  2497. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2498. LinkClosure& lc) const
  2499. {
  2500. bool secondPass = false;
  2501. {
  2502. LinkClosure linkClosure;
  2503. linkClosure.LinkerLanguage = this->LinkerLanguage;
  2504. bool hasHardCodedLinkerLanguage = this->Target->GetProperty("HAS_CXX") ||
  2505. !this->Target->GetSafeProperty("LINKER_LANGUAGE").empty();
  2506. // Get languages built in this target.
  2507. secondPass = this->ComputeLinkClosure(config, linkClosure, false) &&
  2508. !hasHardCodedLinkerLanguage;
  2509. this->LinkerLanguage = linkClosure.LinkerLanguage;
  2510. if (!secondPass) {
  2511. lc = std::move(linkClosure);
  2512. }
  2513. }
  2514. if (secondPass) {
  2515. LinkClosure linkClosure;
  2516. this->ComputeLinkClosure(config, linkClosure, secondPass);
  2517. lc = std::move(linkClosure);
  2518. // linker language must not be changed between the two passes
  2519. if (this->LinkerLanguage != lc.LinkerLanguage) {
  2520. std::ostringstream e;
  2521. e << "Evaluation of $<LINK_LANGUAGE:...> or $<LINK_LAND_AND_ID:...> "
  2522. "changes\nthe linker language for target \""
  2523. << this->GetName() << "\" (from '" << this->LinkerLanguage << "' to '"
  2524. << lc.LinkerLanguage << "') which is invalid.";
  2525. cmSystemTools::Error(e.str());
  2526. }
  2527. }
  2528. }
  2529. cmGeneratorTarget::NameComponents const&
  2530. cmGeneratorTarget::GetFullNameComponents(
  2531. std::string const& config, cmStateEnums::ArtifactType artifact) const
  2532. {
  2533. return this->GetFullNameInternalComponents(config, artifact);
  2534. }
  2535. std::string cmGeneratorTarget::BuildBundleDirectory(
  2536. const std::string& base, const std::string& config,
  2537. BundleDirectoryLevel level) const
  2538. {
  2539. std::string fpath = base;
  2540. if (this->IsAppBundleOnApple()) {
  2541. fpath += this->GetAppBundleDirectory(config, level);
  2542. }
  2543. if (this->IsFrameworkOnApple()) {
  2544. fpath += this->GetFrameworkDirectory(config, level);
  2545. }
  2546. if (this->IsCFBundleOnApple()) {
  2547. fpath += this->GetCFBundleDirectory(config, level);
  2548. }
  2549. return fpath;
  2550. }
  2551. std::string cmGeneratorTarget::GetMacContentDirectory(
  2552. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2553. {
  2554. // Start with the output directory for the target.
  2555. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  2556. BundleDirectoryLevel level = ContentLevel;
  2557. if (this->IsFrameworkOnApple()) {
  2558. // additional files with a framework go into the version specific
  2559. // directory
  2560. level = FullLevel;
  2561. }
  2562. fpath = this->BuildBundleDirectory(fpath, config, level);
  2563. return fpath;
  2564. }
  2565. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  2566. {
  2567. std::string effectiveFolder;
  2568. if (!this->GlobalGenerator->UseFolderProperty()) {
  2569. return effectiveFolder;
  2570. }
  2571. cmValue targetFolder = this->GetProperty("FOLDER");
  2572. if (targetFolder) {
  2573. effectiveFolder += *targetFolder;
  2574. }
  2575. return effectiveFolder;
  2576. }
  2577. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  2578. const std::string& config) const
  2579. {
  2580. // There is no compile information for imported targets.
  2581. if (this->IsImported()) {
  2582. return nullptr;
  2583. }
  2584. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  2585. std::string msg = cmStrCat("cmTarget::GetCompileInfo called for ",
  2586. this->GetName(), " which has type ",
  2587. cmState::GetTargetTypeName(this->GetType()));
  2588. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  2589. return nullptr;
  2590. }
  2591. // Lookup/compute/cache the compile information for this configuration.
  2592. std::string config_upper;
  2593. if (!config.empty()) {
  2594. config_upper = cmSystemTools::UpperCase(config);
  2595. }
  2596. auto i = this->CompileInfoMap.find(config_upper);
  2597. if (i == this->CompileInfoMap.end()) {
  2598. CompileInfo info;
  2599. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  2600. CompileInfoMapType::value_type entry(config_upper, info);
  2601. i = this->CompileInfoMap.insert(entry).first;
  2602. }
  2603. return &i->second;
  2604. }
  2605. cmGeneratorTarget::ModuleDefinitionInfo const*
  2606. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  2607. {
  2608. // A module definition file only makes sense on certain target types.
  2609. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2610. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2611. !this->IsExecutableWithExports()) {
  2612. return nullptr;
  2613. }
  2614. // Lookup/compute/cache the compile information for this configuration.
  2615. std::string config_upper;
  2616. if (!config.empty()) {
  2617. config_upper = cmSystemTools::UpperCase(config);
  2618. }
  2619. auto i = this->ModuleDefinitionInfoMap.find(config_upper);
  2620. if (i == this->ModuleDefinitionInfoMap.end()) {
  2621. ModuleDefinitionInfo info;
  2622. this->ComputeModuleDefinitionInfo(config, info);
  2623. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  2624. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  2625. }
  2626. return &i->second;
  2627. }
  2628. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  2629. std::string const& config, ModuleDefinitionInfo& info) const
  2630. {
  2631. this->GetModuleDefinitionSources(info.Sources, config);
  2632. info.WindowsExportAllSymbols =
  2633. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  2634. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  2635. #if !defined(CMAKE_BOOTSTRAP)
  2636. info.DefFileGenerated =
  2637. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  2638. #else
  2639. // Our __create_def helper is not available during CMake bootstrap.
  2640. info.DefFileGenerated = false;
  2641. #endif
  2642. if (info.DefFileGenerated) {
  2643. info.DefFile =
  2644. this->GetObjectDirectory(config) /* has slash */ + "exports.def";
  2645. } else if (!info.Sources.empty()) {
  2646. info.DefFile = info.Sources.front()->GetFullPath();
  2647. }
  2648. }
  2649. bool cmGeneratorTarget::IsAIX() const
  2650. {
  2651. return this->Target->IsAIX();
  2652. }
  2653. bool cmGeneratorTarget::IsApple() const
  2654. {
  2655. return this->Target->IsApple();
  2656. }
  2657. bool cmGeneratorTarget::IsDLLPlatform() const
  2658. {
  2659. return this->Target->IsDLLPlatform();
  2660. }
  2661. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  2662. const std::string& config) const
  2663. {
  2664. const char* prop =
  2665. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  2666. if (!prop) {
  2667. return;
  2668. }
  2669. cmGeneratorExpressionDAGChecker dagChecker(this, "AUTOUIC_OPTIONS", nullptr,
  2670. nullptr);
  2671. cmExpandList(cmGeneratorExpression::Evaluate(prop, this->LocalGenerator,
  2672. config, this, &dagChecker),
  2673. result);
  2674. }
  2675. static void processILibs(const std::string& config,
  2676. cmGeneratorTarget const* headTarget,
  2677. cmLinkItem const& item, cmGlobalGenerator* gg,
  2678. std::vector<cmGeneratorTarget const*>& tgts,
  2679. std::set<cmGeneratorTarget const*>& emitted)
  2680. {
  2681. if (item.Target && emitted.insert(item.Target).second) {
  2682. tgts.push_back(item.Target);
  2683. if (cmLinkInterfaceLibraries const* iface =
  2684. item.Target->GetLinkInterfaceLibraries(config, headTarget,
  2685. LinkInterfaceFor::Usage)) {
  2686. for (cmLinkItem const& lib : iface->Libraries) {
  2687. processILibs(config, headTarget, lib, gg, tgts, emitted);
  2688. }
  2689. }
  2690. }
  2691. }
  2692. const std::vector<const cmGeneratorTarget*>&
  2693. cmGeneratorTarget::GetLinkImplementationClosure(
  2694. const std::string& config) const
  2695. {
  2696. // There is no link implementation for targets that cannot compile sources.
  2697. if (!this->CanCompileSources()) {
  2698. static std::vector<const cmGeneratorTarget*> const empty;
  2699. return empty;
  2700. }
  2701. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  2702. if (!tgts.Done) {
  2703. tgts.Done = true;
  2704. std::set<cmGeneratorTarget const*> emitted;
  2705. cmLinkImplementationLibraries const* impl =
  2706. this->GetLinkImplementationLibraries(config, LinkInterfaceFor::Usage);
  2707. assert(impl);
  2708. for (cmLinkImplItem const& lib : impl->Libraries) {
  2709. processILibs(config, this, lib,
  2710. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  2711. }
  2712. }
  2713. return tgts;
  2714. }
  2715. class cmTargetTraceDependencies
  2716. {
  2717. public:
  2718. cmTargetTraceDependencies(cmGeneratorTarget* target);
  2719. void Trace();
  2720. private:
  2721. cmGeneratorTarget* GeneratorTarget;
  2722. cmMakefile* Makefile;
  2723. cmLocalGenerator* LocalGenerator;
  2724. cmGlobalGenerator const* GlobalGenerator;
  2725. using SourceEntry = cmGeneratorTarget::SourceEntry;
  2726. SourceEntry* CurrentEntry;
  2727. std::queue<cmSourceFile*> SourceQueue;
  2728. std::set<cmSourceFile*> SourcesQueued;
  2729. using NameMapType = std::map<std::string, cmSourcesWithOutput>;
  2730. NameMapType NameMap;
  2731. std::vector<std::string> NewSources;
  2732. void QueueSource(cmSourceFile* sf);
  2733. void FollowName(std::string const& name);
  2734. void FollowNames(std::vector<std::string> const& names);
  2735. bool IsUtility(std::string const& dep);
  2736. void CheckCustomCommand(cmCustomCommand const& cc);
  2737. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  2738. };
  2739. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  2740. : GeneratorTarget(target)
  2741. {
  2742. // Convenience.
  2743. this->Makefile = target->Target->GetMakefile();
  2744. this->LocalGenerator = target->GetLocalGenerator();
  2745. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  2746. this->CurrentEntry = nullptr;
  2747. // Queue all the source files already specified for the target.
  2748. std::set<cmSourceFile*> emitted;
  2749. std::vector<std::string> const& configs =
  2750. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  2751. for (std::string const& c : configs) {
  2752. std::vector<cmSourceFile*> sources;
  2753. this->GeneratorTarget->GetSourceFiles(sources, c);
  2754. for (cmSourceFile* sf : sources) {
  2755. const std::set<cmGeneratorTarget const*> tgts =
  2756. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  2757. if (cm::contains(tgts, this->GeneratorTarget)) {
  2758. std::ostringstream e;
  2759. e << "Evaluation output file\n \"" << sf->ResolveFullPath()
  2760. << "\"\ndepends on the sources of a target it is used in. This "
  2761. "is a dependency loop and is not allowed.";
  2762. this->GeneratorTarget->LocalGenerator->IssueMessage(
  2763. MessageType::FATAL_ERROR, e.str());
  2764. return;
  2765. }
  2766. if (emitted.insert(sf).second && this->SourcesQueued.insert(sf).second) {
  2767. this->SourceQueue.push(sf);
  2768. }
  2769. }
  2770. }
  2771. // Queue pre-build, pre-link, and post-build rule dependencies.
  2772. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  2773. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  2774. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  2775. }
  2776. void cmTargetTraceDependencies::Trace()
  2777. {
  2778. // Process one dependency at a time until the queue is empty.
  2779. while (!this->SourceQueue.empty()) {
  2780. // Get the next source from the queue.
  2781. cmSourceFile* sf = this->SourceQueue.front();
  2782. this->SourceQueue.pop();
  2783. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  2784. // Queue dependencies added explicitly by the user.
  2785. if (cmValue additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  2786. cmList objDeps{ *additionalDeps };
  2787. for (auto& objDep : objDeps) {
  2788. if (cmSystemTools::FileIsFullPath(objDep)) {
  2789. objDep = cmSystemTools::CollapseFullPath(objDep);
  2790. }
  2791. }
  2792. this->FollowNames(objDeps);
  2793. }
  2794. // Queue the source needed to generate this file, if any.
  2795. this->FollowName(sf->ResolveFullPath());
  2796. // Queue dependencies added programmatically by commands.
  2797. this->FollowNames(sf->GetDepends());
  2798. // Queue custom command dependencies.
  2799. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  2800. this->CheckCustomCommand(*cc);
  2801. }
  2802. }
  2803. this->CurrentEntry = nullptr;
  2804. this->GeneratorTarget->AddTracedSources(this->NewSources);
  2805. }
  2806. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  2807. {
  2808. if (this->SourcesQueued.insert(sf).second) {
  2809. this->SourceQueue.push(sf);
  2810. // Make sure this file is in the target at the end.
  2811. this->NewSources.push_back(sf->ResolveFullPath());
  2812. }
  2813. }
  2814. void cmTargetTraceDependencies::FollowName(std::string const& name)
  2815. {
  2816. // Use lower bound with key comparison to not repeat the search for the
  2817. // insert position if the name could not be found (which is the common case).
  2818. auto i = this->NameMap.lower_bound(name);
  2819. if (i == this->NameMap.end() || i->first != name) {
  2820. // Check if we know how to generate this file.
  2821. cmSourcesWithOutput sources =
  2822. this->LocalGenerator->GetSourcesWithOutput(name);
  2823. // If we failed to find a target or source and we have a relative path, it
  2824. // might be a valid source if made relative to the current binary
  2825. // directory.
  2826. if (!sources.Target && !sources.Source &&
  2827. !cmSystemTools::FileIsFullPath(name)) {
  2828. auto fullname =
  2829. cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/', name);
  2830. fullname = cmSystemTools::CollapseFullPath(
  2831. fullname, this->Makefile->GetHomeOutputDirectory());
  2832. sources = this->LocalGenerator->GetSourcesWithOutput(fullname);
  2833. }
  2834. i = this->NameMap.emplace_hint(i, name, sources);
  2835. }
  2836. if (cmTarget* t = i->second.Target) {
  2837. // The name is a byproduct of a utility target or a PRE_BUILD, PRE_LINK, or
  2838. // POST_BUILD command.
  2839. this->GeneratorTarget->Target->AddUtility(t->GetName(), false);
  2840. }
  2841. if (cmSourceFile* sf = i->second.Source) {
  2842. // For now only follow the dependency if the source file is not a
  2843. // byproduct. Semantics of byproducts in a non-Ninja context will have to
  2844. // be defined first.
  2845. if (!i->second.SourceIsByproduct) {
  2846. // Record the dependency we just followed.
  2847. if (this->CurrentEntry) {
  2848. this->CurrentEntry->Depends.push_back(sf);
  2849. }
  2850. this->QueueSource(sf);
  2851. }
  2852. }
  2853. }
  2854. void cmTargetTraceDependencies::FollowNames(
  2855. std::vector<std::string> const& names)
  2856. {
  2857. for (std::string const& name : names) {
  2858. this->FollowName(name);
  2859. }
  2860. }
  2861. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  2862. {
  2863. // Dependencies on targets (utilities) are supposed to be named by
  2864. // just the target name. However for compatibility we support
  2865. // naming the output file generated by the target (assuming there is
  2866. // no output-name property which old code would not have set). In
  2867. // that case the target name will be the file basename of the
  2868. // dependency.
  2869. std::string util = cmSystemTools::GetFilenameName(dep);
  2870. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  2871. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  2872. }
  2873. // Check for a target with this name.
  2874. if (cmGeneratorTarget* t =
  2875. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  2876. util)) {
  2877. // If we find the target and the dep was given as a full path,
  2878. // then make sure it was not a full path to something else, and
  2879. // the fact that the name matched a target was just a coincidence.
  2880. if (cmSystemTools::FileIsFullPath(dep)) {
  2881. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  2882. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  2883. // This is really only for compatibility so we do not need to
  2884. // worry about configuration names and output names.
  2885. std::string tLocation = t->GetLocationForBuild();
  2886. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  2887. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  2888. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  2889. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  2890. if (depLocation == tLocation) {
  2891. this->GeneratorTarget->Target->AddUtility(util, false);
  2892. return true;
  2893. }
  2894. }
  2895. } else {
  2896. // The original name of the dependency was not a full path. It
  2897. // must name a target, so add the target-level dependency.
  2898. this->GeneratorTarget->Target->AddUtility(util, true);
  2899. return true;
  2900. }
  2901. }
  2902. // The dependency does not name a target built in this project.
  2903. return false;
  2904. }
  2905. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  2906. {
  2907. // Collect dependencies referenced by all configurations.
  2908. std::set<std::string> depends;
  2909. for (std::string const& config :
  2910. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig)) {
  2911. for (cmCustomCommandGenerator const& ccg :
  2912. this->LocalGenerator->MakeCustomCommandGenerators(cc, config)) {
  2913. // Collect target-level dependencies referenced in command lines.
  2914. for (auto const& util : ccg.GetUtilities()) {
  2915. this->GeneratorTarget->Target->AddUtility(util);
  2916. }
  2917. // Collect file-level dependencies referenced in DEPENDS.
  2918. depends.insert(ccg.GetDepends().begin(), ccg.GetDepends().end());
  2919. }
  2920. }
  2921. // Queue file-level dependencies.
  2922. for (std::string const& dep : depends) {
  2923. if (!this->IsUtility(dep)) {
  2924. // The dependency does not name a target and may be a file we
  2925. // know how to generate. Queue it.
  2926. this->FollowName(dep);
  2927. }
  2928. }
  2929. }
  2930. void cmTargetTraceDependencies::CheckCustomCommands(
  2931. const std::vector<cmCustomCommand>& commands)
  2932. {
  2933. for (cmCustomCommand const& command : commands) {
  2934. this->CheckCustomCommand(command);
  2935. }
  2936. }
  2937. void cmGeneratorTarget::TraceDependencies()
  2938. {
  2939. // CMake-generated targets have no dependencies to trace. Normally tracing
  2940. // would find nothing anyway, but when building CMake itself the "install"
  2941. // target command ends up referencing the "cmake" target but we do not
  2942. // really want the dependency because "install" depend on "all" anyway.
  2943. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2944. return;
  2945. }
  2946. // Use a helper object to trace the dependencies.
  2947. cmTargetTraceDependencies tracer(this);
  2948. tracer.Trace();
  2949. }
  2950. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  2951. const std::string& config) const
  2952. {
  2953. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  2954. return info->CompilePdbDir;
  2955. }
  2956. return "";
  2957. }
  2958. std::vector<std::string> cmGeneratorTarget::GetAppleArchs(
  2959. std::string const& config, cm::optional<std::string> lang) const
  2960. {
  2961. cmList archList;
  2962. if (!this->IsApple()) {
  2963. return std::move(archList.data());
  2964. }
  2965. cmValue archs = nullptr;
  2966. if (!config.empty()) {
  2967. std::string defVarName =
  2968. cmStrCat("OSX_ARCHITECTURES_", cmSystemTools::UpperCase(config));
  2969. archs = this->GetProperty(defVarName);
  2970. }
  2971. if (!archs) {
  2972. archs = this->GetProperty("OSX_ARCHITECTURES");
  2973. }
  2974. if (archs) {
  2975. archList.assign(*archs);
  2976. }
  2977. if (archList.empty() &&
  2978. // Fall back to a default architecture if no compiler target is set.
  2979. (!lang ||
  2980. this->Makefile
  2981. ->GetDefinition(cmStrCat("CMAKE_", *lang, "_COMPILER_TARGET"))
  2982. .IsEmpty())) {
  2983. archList.assign(
  2984. this->Makefile->GetDefinition("_CMAKE_APPLE_ARCHS_DEFAULT"));
  2985. }
  2986. return std::move(archList.data());
  2987. }
  2988. void cmGeneratorTarget::AddExplicitLanguageFlags(std::string& flags,
  2989. cmSourceFile const& sf) const
  2990. {
  2991. cmValue lang = sf.GetProperty("LANGUAGE");
  2992. if (!lang) {
  2993. return;
  2994. }
  2995. switch (this->GetPolicyStatusCMP0119()) {
  2996. case cmPolicies::WARN:
  2997. CM_FALLTHROUGH;
  2998. case cmPolicies::OLD:
  2999. // The OLD behavior is to not add explicit language flags.
  3000. return;
  3001. case cmPolicies::REQUIRED_ALWAYS:
  3002. case cmPolicies::REQUIRED_IF_USED:
  3003. case cmPolicies::NEW:
  3004. // The NEW behavior is to add explicit language flags.
  3005. break;
  3006. }
  3007. this->LocalGenerator->AppendFeatureOptions(flags, *lang,
  3008. "EXPLICIT_LANGUAGE");
  3009. }
  3010. void cmGeneratorTarget::AddCUDAArchitectureFlags(cmBuildStep compileOrLink,
  3011. std::string const& config,
  3012. std::string& flags) const
  3013. {
  3014. std::string arch = this->GetSafeProperty("CUDA_ARCHITECTURES");
  3015. if (arch.empty()) {
  3016. switch (this->GetPolicyStatusCMP0104()) {
  3017. case cmPolicies::WARN:
  3018. if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) {
  3019. this->Makefile->IssueMessage(
  3020. MessageType::AUTHOR_WARNING,
  3021. cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) +
  3022. "\nCUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  3023. "\".");
  3024. }
  3025. CM_FALLTHROUGH;
  3026. case cmPolicies::OLD:
  3027. break;
  3028. default:
  3029. this->Makefile->IssueMessage(
  3030. MessageType::FATAL_ERROR,
  3031. "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  3032. "\".");
  3033. }
  3034. }
  3035. // If CUDA_ARCHITECTURES is false we don't add any architectures.
  3036. if (cmIsOff(arch)) {
  3037. return;
  3038. }
  3039. return this->AddCUDAArchitectureFlagsImpl(compileOrLink, config, "CUDA",
  3040. std::move(arch), flags);
  3041. }
  3042. void cmGeneratorTarget::AddCUDAArchitectureFlagsImpl(cmBuildStep compileOrLink,
  3043. std::string const& config,
  3044. std::string const& lang,
  3045. std::string arch,
  3046. std::string& flags) const
  3047. {
  3048. std::string const& compiler = this->Makefile->GetSafeDefinition(
  3049. cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
  3050. const bool ipoEnabled = this->IsIPOEnabled(lang, config);
  3051. // Check for special modes: `all`, `all-major`.
  3052. if (arch == "all" || arch == "all-major") {
  3053. if (compiler == "NVIDIA" &&
  3054. cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  3055. this->Makefile->GetDefinition(cmStrCat(
  3056. "CMAKE_", lang, "_COMPILER_VERSION")),
  3057. "11.5")) {
  3058. flags = cmStrCat(flags, " -arch=", arch);
  3059. return;
  3060. }
  3061. if (arch == "all") {
  3062. arch = *this->Makefile->GetDefinition(
  3063. cmStrCat("CMAKE_", lang, "_ARCHITECTURES_ALL"));
  3064. } else if (arch == "all-major") {
  3065. arch = *this->Makefile->GetDefinition(
  3066. cmStrCat("CMAKE_", lang, "_ARCHITECTURES_ALL_MAJOR"));
  3067. }
  3068. } else if (arch == "native") {
  3069. cmValue native = this->Makefile->GetDefinition(
  3070. cmStrCat("CMAKE_", lang, "_ARCHITECTURES_NATIVE"));
  3071. if (native.IsEmpty()) {
  3072. this->Makefile->IssueMessage(
  3073. MessageType::FATAL_ERROR,
  3074. cmStrCat(lang,
  3075. "_ARCHITECTURES is set to \"native\", but no NVIDIA GPU was "
  3076. "detected."));
  3077. }
  3078. if (compiler == "NVIDIA" &&
  3079. cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  3080. this->Makefile->GetDefinition(cmStrCat(
  3081. "CMAKE_", lang, "_COMPILER_VERSION")),
  3082. "11.6")) {
  3083. flags = cmStrCat(flags, " -arch=", arch);
  3084. return;
  3085. }
  3086. arch = *native;
  3087. }
  3088. struct CudaArchitecture
  3089. {
  3090. std::string name;
  3091. bool real{ true };
  3092. bool virtual_{ true };
  3093. };
  3094. std::vector<CudaArchitecture> architectures;
  3095. {
  3096. cmList options(arch);
  3097. for (auto& option : options) {
  3098. CudaArchitecture architecture;
  3099. // Architecture name is up to the first specifier.
  3100. std::size_t pos = option.find_first_of('-');
  3101. architecture.name = option.substr(0, pos);
  3102. if (pos != std::string::npos) {
  3103. cm::string_view specifier{ option.c_str() + pos + 1,
  3104. option.length() - pos - 1 };
  3105. if (specifier == "real") {
  3106. architecture.real = true;
  3107. architecture.virtual_ = false;
  3108. } else if (specifier == "virtual") {
  3109. architecture.real = false;
  3110. architecture.virtual_ = true;
  3111. } else {
  3112. this->Makefile->IssueMessage(
  3113. MessageType::FATAL_ERROR,
  3114. "Unknown CUDA architecture specifier \"" + std::string(specifier) +
  3115. "\".");
  3116. }
  3117. }
  3118. architectures.emplace_back(architecture);
  3119. }
  3120. }
  3121. if (compiler == "NVIDIA") {
  3122. if (ipoEnabled && compileOrLink == cmBuildStep::Link) {
  3123. if (cmValue cudaIPOFlags = this->Makefile->GetDefinition(
  3124. cmStrCat("CMAKE_", lang, "_LINK_OPTIONS_IPO"))) {
  3125. flags += *cudaIPOFlags;
  3126. }
  3127. }
  3128. for (CudaArchitecture& architecture : architectures) {
  3129. flags +=
  3130. " \"--generate-code=arch=compute_" + architecture.name + ",code=[";
  3131. if (architecture.virtual_) {
  3132. flags += "compute_" + architecture.name;
  3133. if (ipoEnabled || architecture.real) {
  3134. flags += ",";
  3135. }
  3136. }
  3137. if (ipoEnabled) {
  3138. if (compileOrLink == cmBuildStep::Compile) {
  3139. flags += "lto_" + architecture.name;
  3140. } else if (compileOrLink == cmBuildStep::Link) {
  3141. flags += "sm_" + architecture.name;
  3142. }
  3143. } else if (architecture.real) {
  3144. flags += "sm_" + architecture.name;
  3145. }
  3146. flags += "]\"";
  3147. }
  3148. } else if (compiler == "Clang") {
  3149. for (CudaArchitecture& architecture : architectures) {
  3150. flags += " --cuda-gpu-arch=sm_" + architecture.name;
  3151. if (!architecture.real) {
  3152. this->Makefile->IssueMessage(
  3153. MessageType::WARNING,
  3154. "Clang doesn't support disabling CUDA real code generation.");
  3155. }
  3156. if (!architecture.virtual_) {
  3157. flags += " --no-cuda-include-ptx=sm_" + architecture.name;
  3158. }
  3159. }
  3160. }
  3161. }
  3162. void cmGeneratorTarget::AddISPCTargetFlags(std::string& flags) const
  3163. {
  3164. const std::string& arch = this->GetSafeProperty("ISPC_INSTRUCTION_SETS");
  3165. // If ISPC_TARGET is false we don't add any architectures.
  3166. if (cmIsOff(arch)) {
  3167. return;
  3168. }
  3169. std::string const& compiler =
  3170. this->Makefile->GetSafeDefinition("CMAKE_ISPC_COMPILER_ID");
  3171. if (compiler == "Intel") {
  3172. cmList targets(arch);
  3173. if (!targets.empty()) {
  3174. flags += cmStrCat(" --target=", cmWrap("", targets, "", ","));
  3175. }
  3176. }
  3177. }
  3178. void cmGeneratorTarget::AddHIPArchitectureFlags(cmBuildStep compileOrLink,
  3179. std::string const& config,
  3180. std::string& flags) const
  3181. {
  3182. std::string arch = this->GetSafeProperty("HIP_ARCHITECTURES");
  3183. if (arch.empty()) {
  3184. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  3185. "HIP_ARCHITECTURES is empty for target \"" +
  3186. this->GetName() + "\".");
  3187. }
  3188. // If HIP_ARCHITECTURES is false we don't add any architectures.
  3189. if (cmIsOff(arch)) {
  3190. return;
  3191. }
  3192. if (this->Makefile->GetSafeDefinition("CMAKE_HIP_PLATFORM") == "nvidia") {
  3193. return this->AddCUDAArchitectureFlagsImpl(compileOrLink, config, "HIP",
  3194. std::move(arch), flags);
  3195. }
  3196. cmList options(arch);
  3197. for (std::string& option : options) {
  3198. flags += " --offload-arch=" + option;
  3199. }
  3200. }
  3201. void cmGeneratorTarget::AddCUDAToolkitFlags(std::string& flags) const
  3202. {
  3203. std::string const& compiler =
  3204. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  3205. if (compiler == "Clang") {
  3206. // Pass CUDA toolkit explicitly to Clang.
  3207. // Clang's searching for the system CUDA toolkit isn't very good and it's
  3208. // expected the user will explicitly pass the toolkit path.
  3209. // This also avoids Clang having to search for the toolkit on every
  3210. // invocation.
  3211. std::string toolkitRoot =
  3212. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_LIBRARY_ROOT");
  3213. if (!toolkitRoot.empty()) {
  3214. flags += " --cuda-path=" +
  3215. this->LocalGenerator->ConvertToOutputFormat(toolkitRoot,
  3216. cmOutputConverter::SHELL);
  3217. }
  3218. }
  3219. }
  3220. //----------------------------------------------------------------------------
  3221. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  3222. std::string const& var, std::string const& lang,
  3223. std::string const& config) const
  3224. {
  3225. if (this->IsIPOEnabled(lang, config)) {
  3226. std::string varIPO = var + "_IPO";
  3227. if (this->Makefile->IsDefinitionSet(varIPO)) {
  3228. return varIPO;
  3229. }
  3230. }
  3231. return var;
  3232. }
  3233. //----------------------------------------------------------------------------
  3234. std::string cmGeneratorTarget::GetCreateRuleVariable(
  3235. std::string const& lang, std::string const& config) const
  3236. {
  3237. switch (this->GetType()) {
  3238. case cmStateEnums::STATIC_LIBRARY: {
  3239. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  3240. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  3241. }
  3242. case cmStateEnums::SHARED_LIBRARY:
  3243. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  3244. case cmStateEnums::MODULE_LIBRARY:
  3245. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  3246. case cmStateEnums::EXECUTABLE:
  3247. if (this->IsExecutableWithExports()) {
  3248. std::string linkExeWithExports =
  3249. "CMAKE_" + lang + "_LINK_EXECUTABLE_WITH_EXPORTS";
  3250. if (this->Makefile->IsDefinitionSet(linkExeWithExports)) {
  3251. return linkExeWithExports;
  3252. }
  3253. }
  3254. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  3255. default:
  3256. break;
  3257. }
  3258. return "";
  3259. }
  3260. //----------------------------------------------------------------------------
  3261. std::string cmGeneratorTarget::GetClangTidyExportFixesDirectory(
  3262. const std::string& lang) const
  3263. {
  3264. cmValue val =
  3265. this->GetProperty(cmStrCat(lang, "_CLANG_TIDY_EXPORT_FIXES_DIR"));
  3266. if (!cmNonempty(val)) {
  3267. return {};
  3268. }
  3269. std::string path = *val;
  3270. if (!cmSystemTools::FileIsFullPath(path)) {
  3271. path =
  3272. cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', path);
  3273. }
  3274. return cmSystemTools::CollapseFullPath(path);
  3275. }
  3276. namespace {
  3277. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  3278. EvaluatedTargetPropertyEntries& entries,
  3279. std::vector<BT<std::string>>& includes,
  3280. std::unordered_set<std::string>& uniqueIncludes,
  3281. bool debugIncludes)
  3282. {
  3283. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  3284. cmLinkImplItem const& item = entry.LinkImplItem;
  3285. std::string const& targetName = item.AsStr();
  3286. bool const fromImported = item.Target && item.Target->IsImported();
  3287. bool const checkCMP0027 = item.CheckCMP0027;
  3288. std::string usedIncludes;
  3289. for (std::string& entryInclude : entry.Values) {
  3290. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  3291. std::ostringstream e;
  3292. MessageType messageType = MessageType::FATAL_ERROR;
  3293. if (checkCMP0027) {
  3294. switch (tgt->GetPolicyStatusCMP0027()) {
  3295. case cmPolicies::WARN:
  3296. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  3297. CM_FALLTHROUGH;
  3298. case cmPolicies::OLD:
  3299. messageType = MessageType::AUTHOR_WARNING;
  3300. break;
  3301. case cmPolicies::REQUIRED_ALWAYS:
  3302. case cmPolicies::REQUIRED_IF_USED:
  3303. case cmPolicies::NEW:
  3304. break;
  3305. }
  3306. }
  3307. /* clang-format off */
  3308. e << "Imported target \"" << targetName << "\" includes "
  3309. "non-existent path\n \"" << entryInclude << "\"\nin its "
  3310. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  3311. "* The path was deleted, renamed, or moved to another "
  3312. "location.\n"
  3313. "* An install or uninstall procedure did not complete "
  3314. "successfully.\n"
  3315. "* The installation package was faulty and references files it "
  3316. "does not provide.\n";
  3317. /* clang-format on */
  3318. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3319. return;
  3320. }
  3321. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  3322. std::ostringstream e;
  3323. bool noMessage = false;
  3324. MessageType messageType = MessageType::FATAL_ERROR;
  3325. if (!targetName.empty()) {
  3326. /* clang-format off */
  3327. e << "Target \"" << targetName << "\" contains relative "
  3328. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  3329. " \"" << entryInclude << "\"";
  3330. /* clang-format on */
  3331. } else {
  3332. switch (tgt->GetPolicyStatusCMP0021()) {
  3333. case cmPolicies::WARN: {
  3334. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  3335. messageType = MessageType::AUTHOR_WARNING;
  3336. } break;
  3337. case cmPolicies::OLD:
  3338. noMessage = true;
  3339. break;
  3340. case cmPolicies::REQUIRED_IF_USED:
  3341. case cmPolicies::REQUIRED_ALWAYS:
  3342. case cmPolicies::NEW:
  3343. // Issue the fatal message.
  3344. break;
  3345. }
  3346. e << "Found relative path while evaluating include directories of "
  3347. "\""
  3348. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  3349. }
  3350. if (!noMessage) {
  3351. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3352. if (messageType == MessageType::FATAL_ERROR) {
  3353. return;
  3354. }
  3355. }
  3356. }
  3357. if (!cmIsOff(entryInclude)) {
  3358. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  3359. }
  3360. if (uniqueIncludes.insert(entryInclude).second) {
  3361. includes.emplace_back(entryInclude, entry.Backtrace);
  3362. if (debugIncludes) {
  3363. usedIncludes += " * " + entryInclude + "\n";
  3364. }
  3365. }
  3366. }
  3367. if (!usedIncludes.empty()) {
  3368. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3369. MessageType::LOG,
  3370. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  3371. usedIncludes,
  3372. entry.Backtrace);
  3373. }
  3374. }
  3375. }
  3376. }
  3377. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  3378. const std::string& config, const std::string& lang) const
  3379. {
  3380. ConfigAndLanguage cacheKey(config, lang);
  3381. {
  3382. auto it = this->IncludeDirectoriesCache.find(cacheKey);
  3383. if (it != this->IncludeDirectoriesCache.end()) {
  3384. return it->second;
  3385. }
  3386. }
  3387. std::vector<BT<std::string>> includes;
  3388. std::unordered_set<std::string> uniqueIncludes;
  3389. cmGeneratorExpressionDAGChecker dagChecker(this, "INCLUDE_DIRECTORIES",
  3390. nullptr, nullptr);
  3391. cmList debugProperties{ this->Makefile->GetDefinition(
  3392. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  3393. bool debugIncludes = !this->DebugIncludesDone &&
  3394. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  3395. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3396. this->DebugIncludesDone = true;
  3397. }
  3398. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3399. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  3400. if (lang == "Swift") {
  3401. AddLangSpecificImplicitIncludeDirectories(
  3402. this, lang, config, "Swift_MODULE_DIRECTORY",
  3403. IncludeDirectoryFallBack::BINARY, entries);
  3404. }
  3405. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  3406. const std::string propertyName = "ISPC_HEADER_DIRECTORY";
  3407. // If this target has ISPC sources make sure to add the header
  3408. // directory to other compilation units
  3409. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  3410. if (cmValue val = this->GetProperty(propertyName)) {
  3411. includes.emplace_back(*val);
  3412. } else {
  3413. includes.emplace_back(this->GetObjectDirectory(config));
  3414. }
  3415. }
  3416. AddLangSpecificImplicitIncludeDirectories(
  3417. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  3418. entries);
  3419. }
  3420. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  3421. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3422. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  3423. debugIncludes);
  3424. if (this->IsApple()) {
  3425. if (cmLinkImplementationLibraries const* impl =
  3426. this->GetLinkImplementationLibraries(config,
  3427. LinkInterfaceFor::Usage)) {
  3428. for (cmLinkImplItem const& lib : impl->Libraries) {
  3429. std::string libDir;
  3430. if (lib.Target == nullptr) {
  3431. libDir = cmSystemTools::CollapseFullPath(
  3432. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  3433. } else if (lib.Target->Target->IsFrameworkOnApple() ||
  3434. this->IsImportedFrameworkFolderOnApple(config)) {
  3435. libDir = lib.Target->GetLocation(config);
  3436. } else {
  3437. continue;
  3438. }
  3439. auto fwDescriptor =
  3440. this->GetGlobalGenerator()->SplitFrameworkPath(libDir);
  3441. if (!fwDescriptor) {
  3442. continue;
  3443. }
  3444. auto fwInclude = fwDescriptor->GetFrameworkPath();
  3445. if (uniqueIncludes.insert(fwInclude).second) {
  3446. includes.emplace_back(fwInclude, cmListFileBacktrace());
  3447. }
  3448. }
  3449. }
  3450. }
  3451. this->IncludeDirectoriesCache.emplace(cacheKey, includes);
  3452. return includes;
  3453. }
  3454. enum class OptionsParse
  3455. {
  3456. None,
  3457. Shell
  3458. };
  3459. namespace {
  3460. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  3461. const auto DL_END = "</DEVICE_LINK>"_s;
  3462. void processOptions(cmGeneratorTarget const* tgt,
  3463. EvaluatedTargetPropertyEntries const& entries,
  3464. std::vector<BT<std::string>>& options,
  3465. std::unordered_set<std::string>& uniqueOptions,
  3466. bool debugOptions, const char* logName, OptionsParse parse,
  3467. bool processDeviceOptions = false)
  3468. {
  3469. bool splitOption = !processDeviceOptions;
  3470. for (EvaluatedTargetPropertyEntry const& entry : entries.Entries) {
  3471. std::string usedOptions;
  3472. for (std::string const& opt : entry.Values) {
  3473. if (processDeviceOptions && (opt == DL_BEGIN || opt == DL_END)) {
  3474. options.emplace_back(opt, entry.Backtrace);
  3475. splitOption = opt == DL_BEGIN;
  3476. continue;
  3477. }
  3478. if (uniqueOptions.insert(opt).second) {
  3479. if (parse == OptionsParse::Shell &&
  3480. cmHasLiteralPrefix(opt, "SHELL:")) {
  3481. if (splitOption) {
  3482. std::vector<std::string> tmp;
  3483. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  3484. for (std::string& o : tmp) {
  3485. options.emplace_back(std::move(o), entry.Backtrace);
  3486. }
  3487. } else {
  3488. options.emplace_back(std::string(opt.c_str() + 6),
  3489. entry.Backtrace);
  3490. }
  3491. } else {
  3492. options.emplace_back(opt, entry.Backtrace);
  3493. }
  3494. if (debugOptions) {
  3495. usedOptions += " * " + opt + "\n";
  3496. }
  3497. }
  3498. }
  3499. if (!usedOptions.empty()) {
  3500. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3501. MessageType::LOG,
  3502. std::string("Used ") + logName + std::string(" for target ") +
  3503. tgt->GetName() + ":\n" + usedOptions,
  3504. entry.Backtrace);
  3505. }
  3506. }
  3507. }
  3508. std::vector<BT<std::string>> wrapOptions(
  3509. std::vector<std::string>& options, const cmListFileBacktrace& bt,
  3510. const std::vector<std::string>& wrapperFlag, const std::string& wrapperSep,
  3511. bool concatFlagAndArgs)
  3512. {
  3513. std::vector<BT<std::string>> result;
  3514. if (options.empty()) {
  3515. return result;
  3516. }
  3517. if (wrapperFlag.empty()) {
  3518. // nothing specified, insert elements as is
  3519. result.reserve(options.size());
  3520. for (std::string& o : options) {
  3521. result.emplace_back(std::move(o), bt);
  3522. }
  3523. return result;
  3524. }
  3525. for (std::vector<std::string>::size_type index = 0; index < options.size();
  3526. index++) {
  3527. if (cmHasLiteralPrefix(options[index], "LINKER:")) {
  3528. // LINKER wrapper specified, insert elements as is
  3529. result.emplace_back(std::move(options[index]), bt);
  3530. continue;
  3531. }
  3532. if (cmHasLiteralPrefix(options[index], "-Wl,")) {
  3533. // replace option by LINKER wrapper
  3534. result.emplace_back(options[index].replace(0, 4, "LINKER:"), bt);
  3535. continue;
  3536. }
  3537. if (cmHasLiteralPrefix(options[index], "-Xlinker=")) {
  3538. // replace option by LINKER wrapper
  3539. result.emplace_back(options[index].replace(0, 9, "LINKER:"), bt);
  3540. continue;
  3541. }
  3542. if (options[index] == "-Xlinker") {
  3543. // replace option by LINKER wrapper
  3544. if (index + 1 < options.size()) {
  3545. result.emplace_back("LINKER:" + options[++index], bt);
  3546. } else {
  3547. result.emplace_back(std::move(options[index]), bt);
  3548. }
  3549. continue;
  3550. }
  3551. // collect all options which must be transformed
  3552. std::vector<std::string> opts;
  3553. while (index < options.size()) {
  3554. if (!cmHasLiteralPrefix(options[index], "LINKER:") &&
  3555. !cmHasLiteralPrefix(options[index], "-Wl,") &&
  3556. !cmHasLiteralPrefix(options[index], "-Xlinker")) {
  3557. opts.emplace_back(std::move(options[index++]));
  3558. } else {
  3559. --index;
  3560. break;
  3561. }
  3562. }
  3563. if (opts.empty()) {
  3564. continue;
  3565. }
  3566. if (!wrapperSep.empty()) {
  3567. if (concatFlagAndArgs) {
  3568. // insert flag elements except last one
  3569. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  3570. result.emplace_back(*i, bt);
  3571. }
  3572. // concatenate last flag element and all list values
  3573. // in one option
  3574. result.emplace_back(wrapperFlag.back() + cmJoin(opts, wrapperSep), bt);
  3575. } else {
  3576. for (std::string const& i : wrapperFlag) {
  3577. result.emplace_back(i, bt);
  3578. }
  3579. // concatenate all list values in one option
  3580. result.emplace_back(cmJoin(opts, wrapperSep), bt);
  3581. }
  3582. } else {
  3583. // prefix each element of list with wrapper
  3584. if (concatFlagAndArgs) {
  3585. std::transform(opts.begin(), opts.end(), opts.begin(),
  3586. [&wrapperFlag](std::string const& o) -> std::string {
  3587. return wrapperFlag.back() + o;
  3588. });
  3589. }
  3590. for (std::string& o : opts) {
  3591. for (auto i = wrapperFlag.begin(),
  3592. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  3593. : wrapperFlag.end();
  3594. i != e; ++i) {
  3595. result.emplace_back(*i, bt);
  3596. }
  3597. result.emplace_back(std::move(o), bt);
  3598. }
  3599. }
  3600. }
  3601. return result;
  3602. }
  3603. }
  3604. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  3605. const std::string& config,
  3606. const std::string& language) const
  3607. {
  3608. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  3609. result.reserve(tmp.size());
  3610. for (BT<std::string>& v : tmp) {
  3611. result.emplace_back(std::move(v.Value));
  3612. }
  3613. }
  3614. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  3615. std::string const& config, std::string const& language) const
  3616. {
  3617. ConfigAndLanguage cacheKey(config, language);
  3618. {
  3619. auto it = this->CompileOptionsCache.find(cacheKey);
  3620. if (it != this->CompileOptionsCache.end()) {
  3621. return it->second;
  3622. }
  3623. }
  3624. std::vector<BT<std::string>> result;
  3625. std::unordered_set<std::string> uniqueOptions;
  3626. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_OPTIONS", nullptr,
  3627. nullptr);
  3628. cmList debugProperties{ this->Makefile->GetDefinition(
  3629. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  3630. bool debugOptions = !this->DebugCompileOptionsDone &&
  3631. cm::contains(debugProperties, "COMPILE_OPTIONS");
  3632. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3633. this->DebugCompileOptionsDone = true;
  3634. }
  3635. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3636. this, config, language, &dagChecker, this->CompileOptionsEntries);
  3637. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS", language,
  3638. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3639. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3640. "compile options", OptionsParse::Shell);
  3641. CompileOptionsCache.emplace(cacheKey, result);
  3642. return result;
  3643. }
  3644. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  3645. const std::string& config) const
  3646. {
  3647. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  3648. result.reserve(tmp.size());
  3649. for (BT<std::string>& v : tmp) {
  3650. result.emplace_back(std::move(v.Value));
  3651. }
  3652. }
  3653. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  3654. std::string const& config) const
  3655. {
  3656. std::vector<BT<std::string>> result;
  3657. std::unordered_set<std::string> uniqueFeatures;
  3658. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_FEATURES", nullptr,
  3659. nullptr);
  3660. cmList debugProperties{ this->Makefile->GetDefinition(
  3661. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  3662. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  3663. cm::contains(debugProperties, "COMPILE_FEATURES");
  3664. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3665. this->DebugCompileFeaturesDone = true;
  3666. }
  3667. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3668. this, config, std::string(), &dagChecker, this->CompileFeaturesEntries);
  3669. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  3670. std::string(), &dagChecker, entries,
  3671. IncludeRuntimeInterface::Yes);
  3672. processOptions(this, entries, result, uniqueFeatures, debugFeatures,
  3673. "compile features", OptionsParse::None);
  3674. return result;
  3675. }
  3676. void cmGeneratorTarget::GetCompileDefinitions(
  3677. std::vector<std::string>& result, const std::string& config,
  3678. const std::string& language) const
  3679. {
  3680. std::vector<BT<std::string>> tmp =
  3681. this->GetCompileDefinitions(config, language);
  3682. result.reserve(tmp.size());
  3683. for (BT<std::string>& v : tmp) {
  3684. result.emplace_back(std::move(v.Value));
  3685. }
  3686. }
  3687. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  3688. std::string const& config, std::string const& language) const
  3689. {
  3690. ConfigAndLanguage cacheKey(config, language);
  3691. {
  3692. auto it = this->CompileDefinitionsCache.find(cacheKey);
  3693. if (it != this->CompileDefinitionsCache.end()) {
  3694. return it->second;
  3695. }
  3696. }
  3697. std::vector<BT<std::string>> list;
  3698. std::unordered_set<std::string> uniqueOptions;
  3699. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  3700. nullptr, nullptr);
  3701. cmList debugProperties{ this->Makefile->GetDefinition(
  3702. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  3703. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  3704. cm::contains(debugProperties, "COMPILE_DEFINITIONS");
  3705. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3706. this->DebugCompileDefinitionsDone = true;
  3707. }
  3708. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3709. this, config, language, &dagChecker, this->CompileDefinitionsEntries);
  3710. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS", language,
  3711. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3712. if (!config.empty()) {
  3713. std::string configPropName =
  3714. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  3715. cmValue configProp = this->GetProperty(configPropName);
  3716. if (configProp) {
  3717. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  3718. case cmPolicies::WARN: {
  3719. this->LocalGenerator->IssueMessage(
  3720. MessageType::AUTHOR_WARNING,
  3721. cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
  3722. CM_FALLTHROUGH;
  3723. }
  3724. case cmPolicies::OLD: {
  3725. std::unique_ptr<TargetPropertyEntry> entry =
  3726. CreateTargetPropertyEntry(
  3727. *this->LocalGenerator->GetCMakeInstance(), *configProp);
  3728. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3729. this, config, language, &dagChecker, *entry));
  3730. } break;
  3731. case cmPolicies::NEW:
  3732. case cmPolicies::REQUIRED_ALWAYS:
  3733. case cmPolicies::REQUIRED_IF_USED:
  3734. break;
  3735. }
  3736. }
  3737. }
  3738. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3739. "compile definitions", OptionsParse::None);
  3740. this->CompileDefinitionsCache.emplace(cacheKey, list);
  3741. return list;
  3742. }
  3743. std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
  3744. const std::string& config, const std::string& language) const
  3745. {
  3746. ConfigAndLanguage cacheKey(config, language);
  3747. {
  3748. auto it = this->PrecompileHeadersCache.find(cacheKey);
  3749. if (it != this->PrecompileHeadersCache.end()) {
  3750. return it->second;
  3751. }
  3752. }
  3753. std::unordered_set<std::string> uniqueOptions;
  3754. cmGeneratorExpressionDAGChecker dagChecker(this, "PRECOMPILE_HEADERS",
  3755. nullptr, nullptr);
  3756. cmList debugProperties{ this->Makefile->GetDefinition(
  3757. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  3758. bool debugDefines = !this->DebugPrecompileHeadersDone &&
  3759. std::find(debugProperties.begin(), debugProperties.end(),
  3760. "PRECOMPILE_HEADERS") != debugProperties.end();
  3761. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3762. this->DebugPrecompileHeadersDone = true;
  3763. }
  3764. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3765. this, config, language, &dagChecker, this->PrecompileHeadersEntries);
  3766. AddInterfaceEntries(this, config, "INTERFACE_PRECOMPILE_HEADERS", language,
  3767. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  3768. std::vector<BT<std::string>> list;
  3769. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3770. "precompile headers", OptionsParse::None);
  3771. this->PrecompileHeadersCache.emplace(cacheKey, list);
  3772. return list;
  3773. }
  3774. std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
  3775. const std::string& language,
  3776. const std::string& arch) const
  3777. {
  3778. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3779. language != "OBJCXX") {
  3780. return std::string();
  3781. }
  3782. if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
  3783. return std::string();
  3784. }
  3785. const cmGeneratorTarget* generatorTarget = this;
  3786. cmValue pchReuseFrom =
  3787. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3788. const auto inserted =
  3789. this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
  3790. if (inserted.second) {
  3791. const std::vector<BT<std::string>> headers =
  3792. this->GetPrecompileHeaders(config, language);
  3793. if (headers.empty() && !pchReuseFrom) {
  3794. return std::string();
  3795. }
  3796. std::string& filename = inserted.first->second;
  3797. if (pchReuseFrom) {
  3798. generatorTarget =
  3799. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3800. }
  3801. const std::map<std::string, std::string> languageToExtension = {
  3802. { "C", ".h" },
  3803. { "CXX", ".hxx" },
  3804. { "OBJC", ".objc.h" },
  3805. { "OBJCXX", ".objcxx.hxx" }
  3806. };
  3807. filename = generatorTarget->GetSupportDirectory();
  3808. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3809. filename = cmStrCat(filename, "/", config);
  3810. }
  3811. filename =
  3812. cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat("_", arch),
  3813. languageToExtension.at(language));
  3814. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3815. if (!pchReuseFrom) {
  3816. cmValue pchPrologue =
  3817. this->Makefile->GetDefinition("CMAKE_PCH_PROLOGUE");
  3818. cmValue pchEpilogue =
  3819. this->Makefile->GetDefinition("CMAKE_PCH_EPILOGUE");
  3820. std::string firstHeaderOnDisk;
  3821. {
  3822. cmGeneratedFileStream file(
  3823. filename_tmp, false,
  3824. this->GetGlobalGenerator()->GetMakefileEncoding());
  3825. file << "/* generated by CMake */\n\n";
  3826. if (pchPrologue) {
  3827. file << *pchPrologue << "\n";
  3828. }
  3829. if (this->GetGlobalGenerator()->IsXcode()) {
  3830. file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3831. }
  3832. if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
  3833. file << "#ifdef __cplusplus\n";
  3834. }
  3835. for (auto const& header_bt : headers) {
  3836. if (header_bt.Value.empty()) {
  3837. continue;
  3838. }
  3839. if (header_bt.Value[0] == '<' || header_bt.Value[0] == '\"') {
  3840. file << "#include " << header_bt.Value << "\n";
  3841. } else {
  3842. file << "#include \"" << header_bt.Value << "\"\n";
  3843. }
  3844. if (cmSystemTools::FileExists(header_bt.Value) &&
  3845. firstHeaderOnDisk.empty()) {
  3846. firstHeaderOnDisk = header_bt.Value;
  3847. }
  3848. }
  3849. if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
  3850. file << "#endif // __cplusplus\n";
  3851. }
  3852. if (this->GetGlobalGenerator()->IsXcode()) {
  3853. file << "#endif // CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3854. }
  3855. if (pchEpilogue) {
  3856. file << *pchEpilogue << "\n";
  3857. }
  3858. }
  3859. if (!firstHeaderOnDisk.empty()) {
  3860. cmFileTimes::Copy(firstHeaderOnDisk, filename_tmp);
  3861. }
  3862. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3863. }
  3864. }
  3865. return inserted.first->second;
  3866. }
  3867. std::string cmGeneratorTarget::GetPchSource(const std::string& config,
  3868. const std::string& language,
  3869. const std::string& arch) const
  3870. {
  3871. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3872. language != "OBJCXX") {
  3873. return std::string();
  3874. }
  3875. const auto inserted =
  3876. this->PchSources.insert(std::make_pair(language + config + arch, ""));
  3877. if (inserted.second) {
  3878. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3879. if (pchHeader.empty()) {
  3880. return std::string();
  3881. }
  3882. std::string& filename = inserted.first->second;
  3883. const cmGeneratorTarget* generatorTarget = this;
  3884. cmValue pchReuseFrom =
  3885. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3886. if (pchReuseFrom) {
  3887. generatorTarget =
  3888. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3889. }
  3890. filename = cmStrCat(generatorTarget->GetSupportDirectory(), "/cmake_pch");
  3891. // For GCC the source extension will be transformed into .h[xx].gch
  3892. if (!this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3893. const std::map<std::string, std::string> languageToExtension = {
  3894. { "C", ".h.c" },
  3895. { "CXX", ".hxx.cxx" },
  3896. { "OBJC", ".objc.h.m" },
  3897. { "OBJCXX", ".objcxx.hxx.mm" }
  3898. };
  3899. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3900. languageToExtension.at(language));
  3901. } else {
  3902. const std::map<std::string, std::string> languageToExtension = {
  3903. { "C", ".c" }, { "CXX", ".cxx" }, { "OBJC", ".m" }, { "OBJCXX", ".mm" }
  3904. };
  3905. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3906. languageToExtension.at(language));
  3907. }
  3908. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3909. if (!pchReuseFrom) {
  3910. {
  3911. cmGeneratedFileStream file(filename_tmp);
  3912. file << "/* generated by CMake */\n";
  3913. }
  3914. cmFileTimes::Copy(pchHeader, filename_tmp);
  3915. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3916. }
  3917. }
  3918. return inserted.first->second;
  3919. }
  3920. std::string cmGeneratorTarget::GetPchFileObject(const std::string& config,
  3921. const std::string& language,
  3922. const std::string& arch)
  3923. {
  3924. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3925. language != "OBJCXX") {
  3926. return std::string();
  3927. }
  3928. const auto inserted =
  3929. this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
  3930. if (inserted.second) {
  3931. const std::string pchSource = this->GetPchSource(config, language, arch);
  3932. if (pchSource.empty()) {
  3933. return std::string();
  3934. }
  3935. std::string& filename = inserted.first->second;
  3936. auto* pchSf = this->Makefile->GetOrCreateSource(
  3937. pchSource, false, cmSourceFileLocationKind::Known);
  3938. filename = cmStrCat(this->ObjectDirectory, this->GetObjectName(pchSf));
  3939. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3940. cmSystemTools::ReplaceString(
  3941. filename, this->GetGlobalGenerator()->GetCMakeCFGIntDir(), config);
  3942. }
  3943. }
  3944. return inserted.first->second;
  3945. }
  3946. std::string cmGeneratorTarget::GetPchFile(const std::string& config,
  3947. const std::string& language,
  3948. const std::string& arch)
  3949. {
  3950. const auto inserted =
  3951. this->PchFiles.insert(std::make_pair(language + config + arch, ""));
  3952. if (inserted.second) {
  3953. std::string& pchFile = inserted.first->second;
  3954. const std::string pchExtension =
  3955. this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
  3956. if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3957. auto replaceExtension = [](const std::string& str,
  3958. const std::string& ext) -> std::string {
  3959. auto dot_pos = str.rfind('.');
  3960. std::string result;
  3961. if (dot_pos != std::string::npos) {
  3962. result = str.substr(0, dot_pos);
  3963. }
  3964. result += ext;
  3965. return result;
  3966. };
  3967. cmGeneratorTarget* generatorTarget = this;
  3968. cmValue pchReuseFrom =
  3969. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3970. if (pchReuseFrom) {
  3971. generatorTarget =
  3972. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3973. }
  3974. const std::string pchFileObject =
  3975. generatorTarget->GetPchFileObject(config, language, arch);
  3976. if (!pchExtension.empty()) {
  3977. pchFile = replaceExtension(pchFileObject, pchExtension);
  3978. }
  3979. } else {
  3980. pchFile = this->GetPchHeader(config, language, arch);
  3981. pchFile += pchExtension;
  3982. }
  3983. }
  3984. return inserted.first->second;
  3985. }
  3986. std::string cmGeneratorTarget::GetPchCreateCompileOptions(
  3987. const std::string& config, const std::string& language,
  3988. const std::string& arch)
  3989. {
  3990. const auto inserted = this->PchCreateCompileOptions.insert(
  3991. std::make_pair(language + config + arch, ""));
  3992. if (inserted.second) {
  3993. std::string& createOptionList = inserted.first->second;
  3994. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  3995. createOptionList = this->Makefile->GetSafeDefinition(
  3996. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  3997. }
  3998. if (this->GetPropertyAsBool("PCH_INSTANTIATE_TEMPLATES")) {
  3999. std::string varName = cmStrCat(
  4000. "CMAKE_", language, "_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH");
  4001. std::string instantiateOption =
  4002. this->Makefile->GetSafeDefinition(varName);
  4003. if (!instantiateOption.empty()) {
  4004. createOptionList = cmStrCat(createOptionList, ";", instantiateOption);
  4005. }
  4006. }
  4007. const std::string createOptVar =
  4008. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
  4009. createOptionList = cmStrCat(
  4010. createOptionList, ";", this->Makefile->GetSafeDefinition(createOptVar));
  4011. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  4012. const std::string pchFile = this->GetPchFile(config, language, arch);
  4013. cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>", pchHeader);
  4014. cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>", pchFile);
  4015. }
  4016. return inserted.first->second;
  4017. }
  4018. std::string cmGeneratorTarget::GetPchUseCompileOptions(
  4019. const std::string& config, const std::string& language,
  4020. const std::string& arch)
  4021. {
  4022. const auto inserted = this->PchUseCompileOptions.insert(
  4023. std::make_pair(language + config + arch, ""));
  4024. if (inserted.second) {
  4025. std::string& useOptionList = inserted.first->second;
  4026. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  4027. useOptionList = this->Makefile->GetSafeDefinition(
  4028. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  4029. }
  4030. const std::string useOptVar =
  4031. cmStrCat(language, "_COMPILE_OPTIONS_USE_PCH");
  4032. std::string const& useOptionListProperty =
  4033. this->GetSafeProperty(useOptVar);
  4034. useOptionList = cmStrCat(
  4035. useOptionList, ";",
  4036. useOptionListProperty.empty()
  4037. ? this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", useOptVar))
  4038. : useOptionListProperty);
  4039. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  4040. const std::string pchFile = this->GetPchFile(config, language, arch);
  4041. cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>", pchHeader);
  4042. cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>", pchFile);
  4043. }
  4044. return inserted.first->second;
  4045. }
  4046. void cmGeneratorTarget::AddSourceFileToUnityBatch(
  4047. const std::string& sourceFilename)
  4048. {
  4049. this->UnityBatchedSourceFiles.insert(sourceFilename);
  4050. }
  4051. bool cmGeneratorTarget::IsSourceFilePartOfUnityBatch(
  4052. const std::string& sourceFilename) const
  4053. {
  4054. if (!this->GetPropertyAsBool("UNITY_BUILD")) {
  4055. return false;
  4056. }
  4057. return this->UnityBatchedSourceFiles.find(sourceFilename) !=
  4058. this->UnityBatchedSourceFiles.end();
  4059. }
  4060. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  4061. const std::string& config,
  4062. const std::string& language) const
  4063. {
  4064. if (this->IsDeviceLink() &&
  4065. this->GetPolicyStatusCMP0105() != cmPolicies::NEW) {
  4066. // link options are not propagated to the device link step
  4067. return;
  4068. }
  4069. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  4070. result.reserve(tmp.size());
  4071. for (BT<std::string>& v : tmp) {
  4072. result.emplace_back(std::move(v.Value));
  4073. }
  4074. }
  4075. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  4076. std::string const& config, std::string const& language) const
  4077. {
  4078. ConfigAndLanguage cacheKey(
  4079. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  4080. {
  4081. auto it = this->LinkOptionsCache.find(cacheKey);
  4082. if (it != this->LinkOptionsCache.end()) {
  4083. return it->second;
  4084. }
  4085. }
  4086. std::vector<BT<std::string>> result;
  4087. std::unordered_set<std::string> uniqueOptions;
  4088. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_OPTIONS", nullptr,
  4089. nullptr);
  4090. cmList debugProperties{ this->Makefile->GetDefinition(
  4091. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  4092. bool debugOptions = !this->DebugLinkOptionsDone &&
  4093. cm::contains(debugProperties, "LINK_OPTIONS");
  4094. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4095. this->DebugLinkOptionsDone = true;
  4096. }
  4097. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  4098. this, config, language, &dagChecker, this->LinkOptionsEntries);
  4099. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS", language,
  4100. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4101. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4102. ? LinkInterfaceFor::Link
  4103. : LinkInterfaceFor::Usage);
  4104. processOptions(this, entries, result, uniqueOptions, debugOptions,
  4105. "link options", OptionsParse::Shell, this->IsDeviceLink());
  4106. if (this->IsDeviceLink()) {
  4107. // wrap host link options
  4108. const std::string wrapper(this->Makefile->GetSafeDefinition(
  4109. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG"));
  4110. cmList wrapperFlag{ wrapper };
  4111. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  4112. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG_SEP"));
  4113. bool concatFlagAndArgs = true;
  4114. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  4115. concatFlagAndArgs = false;
  4116. wrapperFlag.pop_back();
  4117. }
  4118. auto it = result.begin();
  4119. while (it != result.end()) {
  4120. if (it->Value == DL_BEGIN) {
  4121. // device link options, no treatment
  4122. it = result.erase(it);
  4123. it = std::find_if(it, result.end(), [](const BT<std::string>& item) {
  4124. return item.Value == DL_END;
  4125. });
  4126. if (it != result.end()) {
  4127. it = result.erase(it);
  4128. }
  4129. } else {
  4130. // host link options must be wrapped
  4131. std::vector<std::string> options;
  4132. cmSystemTools::ParseUnixCommandLine(it->Value.c_str(), options);
  4133. auto hostOptions = wrapOptions(options, it->Backtrace, wrapperFlag,
  4134. wrapperSep, concatFlagAndArgs);
  4135. it = result.erase(it);
  4136. // some compilers (like gcc 4.8 or Intel 19.0 or XLC 16) do not respect
  4137. // C++11 standard: 'std::vector::insert()' do not returns an iterator,
  4138. // so need to recompute the iterator after insertion.
  4139. if (it == result.end()) {
  4140. cm::append(result, hostOptions);
  4141. it = result.end();
  4142. } else {
  4143. auto index = it - result.begin();
  4144. result.insert(it, hostOptions.begin(), hostOptions.end());
  4145. it = result.begin() + index + hostOptions.size();
  4146. }
  4147. }
  4148. }
  4149. }
  4150. // Last step: replace "LINKER:" prefixed elements by
  4151. // actual linker wrapper
  4152. result = this->ResolveLinkerWrapper(result, language);
  4153. this->LinkOptionsCache.emplace(cacheKey, result);
  4154. return result;
  4155. }
  4156. std::vector<BT<std::string>>& cmGeneratorTarget::ResolveLinkerWrapper(
  4157. std::vector<BT<std::string>>& result, const std::string& language,
  4158. bool joinItems) const
  4159. {
  4160. // replace "LINKER:" prefixed elements by actual linker wrapper
  4161. const std::string wrapper(this->Makefile->GetSafeDefinition(
  4162. "CMAKE_" + language +
  4163. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG"
  4164. : "_LINKER_WRAPPER_FLAG")));
  4165. cmList wrapperFlag{ wrapper };
  4166. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  4167. "CMAKE_" + language +
  4168. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG_SEP"
  4169. : "_LINKER_WRAPPER_FLAG_SEP")));
  4170. bool concatFlagAndArgs = true;
  4171. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  4172. concatFlagAndArgs = false;
  4173. wrapperFlag.pop_back();
  4174. }
  4175. const std::string LINKER{ "LINKER:" };
  4176. const std::string SHELL{ "SHELL:" };
  4177. const std::string LINKER_SHELL = LINKER + SHELL;
  4178. std::vector<BT<std::string>>::iterator entry;
  4179. while ((entry = std::find_if(result.begin(), result.end(),
  4180. [&LINKER](BT<std::string> const& item) -> bool {
  4181. return item.Value.compare(0, LINKER.length(),
  4182. LINKER) == 0;
  4183. })) != result.end()) {
  4184. std::string value = std::move(entry->Value);
  4185. cmListFileBacktrace bt = std::move(entry->Backtrace);
  4186. entry = result.erase(entry);
  4187. std::vector<std::string> linkerOptions;
  4188. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  4189. cmSystemTools::ParseUnixCommandLine(
  4190. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  4191. } else {
  4192. linkerOptions = cmTokenize(value.substr(LINKER.length()), ",");
  4193. }
  4194. if (linkerOptions.empty() ||
  4195. (linkerOptions.size() == 1 && linkerOptions.front().empty())) {
  4196. continue;
  4197. }
  4198. // for now, raise an error if prefix SHELL: is part of arguments
  4199. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  4200. [&SHELL](const std::string& item) -> bool {
  4201. return item.find(SHELL) != std::string::npos;
  4202. }) != linkerOptions.end()) {
  4203. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4204. MessageType::FATAL_ERROR,
  4205. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  4206. this->GetBacktrace());
  4207. return result;
  4208. }
  4209. std::vector<BT<std::string>> options = wrapOptions(
  4210. linkerOptions, bt, wrapperFlag, wrapperSep, concatFlagAndArgs);
  4211. if (joinItems) {
  4212. result.insert(entry,
  4213. cmJoin(cmRange<decltype(options.cbegin())>(
  4214. options.cbegin(), options.cend()),
  4215. " "_s));
  4216. } else {
  4217. result.insert(entry, options.begin(), options.end());
  4218. }
  4219. }
  4220. return result;
  4221. }
  4222. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  4223. std::vector<std::string>& result, const std::string& config,
  4224. const std::string& language) const
  4225. {
  4226. std::vector<BT<std::string>> tmp =
  4227. this->GetStaticLibraryLinkOptions(config, language);
  4228. result.reserve(tmp.size());
  4229. for (BT<std::string>& v : tmp) {
  4230. result.emplace_back(std::move(v.Value));
  4231. }
  4232. }
  4233. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  4234. std::string const& config, std::string const& language) const
  4235. {
  4236. std::vector<BT<std::string>> result;
  4237. std::unordered_set<std::string> uniqueOptions;
  4238. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  4239. nullptr, nullptr);
  4240. EvaluatedTargetPropertyEntries entries;
  4241. if (cmValue linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  4242. std::unique_ptr<TargetPropertyEntry> entry = CreateTargetPropertyEntry(
  4243. *this->LocalGenerator->GetCMakeInstance(), *linkOptions);
  4244. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  4245. this, config, language, &dagChecker, *entry));
  4246. }
  4247. processOptions(this, entries, result, uniqueOptions, false,
  4248. "static library link options", OptionsParse::Shell);
  4249. return result;
  4250. }
  4251. namespace {
  4252. void processLinkDirectories(cmGeneratorTarget const* tgt,
  4253. EvaluatedTargetPropertyEntries& entries,
  4254. std::vector<BT<std::string>>& directories,
  4255. std::unordered_set<std::string>& uniqueDirectories,
  4256. bool debugDirectories)
  4257. {
  4258. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  4259. cmLinkImplItem const& item = entry.LinkImplItem;
  4260. std::string const& targetName = item.AsStr();
  4261. std::string usedDirectories;
  4262. for (std::string& entryDirectory : entry.Values) {
  4263. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  4264. std::ostringstream e;
  4265. bool noMessage = false;
  4266. MessageType messageType = MessageType::FATAL_ERROR;
  4267. if (!targetName.empty()) {
  4268. /* clang-format off */
  4269. e << "Target \"" << targetName << "\" contains relative "
  4270. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  4271. " \"" << entryDirectory << "\"";
  4272. /* clang-format on */
  4273. } else {
  4274. switch (tgt->GetPolicyStatusCMP0081()) {
  4275. case cmPolicies::WARN: {
  4276. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  4277. messageType = MessageType::AUTHOR_WARNING;
  4278. } break;
  4279. case cmPolicies::OLD:
  4280. noMessage = true;
  4281. break;
  4282. case cmPolicies::REQUIRED_IF_USED:
  4283. case cmPolicies::REQUIRED_ALWAYS:
  4284. case cmPolicies::NEW:
  4285. // Issue the fatal message.
  4286. break;
  4287. }
  4288. e << "Found relative path while evaluating link directories of "
  4289. "\""
  4290. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  4291. }
  4292. if (!noMessage) {
  4293. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  4294. if (messageType == MessageType::FATAL_ERROR) {
  4295. return;
  4296. }
  4297. }
  4298. }
  4299. // Sanitize the path the same way the link_directories command does
  4300. // in case projects set the LINK_DIRECTORIES property directly.
  4301. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  4302. if (uniqueDirectories.insert(entryDirectory).second) {
  4303. directories.emplace_back(entryDirectory, entry.Backtrace);
  4304. if (debugDirectories) {
  4305. usedDirectories += " * " + entryDirectory + "\n";
  4306. }
  4307. }
  4308. }
  4309. if (!usedDirectories.empty()) {
  4310. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  4311. MessageType::LOG,
  4312. std::string("Used link directories for target ") + tgt->GetName() +
  4313. ":\n" + usedDirectories,
  4314. entry.Backtrace);
  4315. }
  4316. }
  4317. }
  4318. }
  4319. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  4320. const std::string& config,
  4321. const std::string& language) const
  4322. {
  4323. std::vector<BT<std::string>> tmp =
  4324. this->GetLinkDirectories(config, language);
  4325. result.reserve(tmp.size());
  4326. for (BT<std::string>& v : tmp) {
  4327. result.emplace_back(std::move(v.Value));
  4328. }
  4329. }
  4330. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  4331. std::string const& config, std::string const& language) const
  4332. {
  4333. ConfigAndLanguage cacheKey(
  4334. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  4335. {
  4336. auto it = this->LinkDirectoriesCache.find(cacheKey);
  4337. if (it != this->LinkDirectoriesCache.end()) {
  4338. return it->second;
  4339. }
  4340. }
  4341. std::vector<BT<std::string>> result;
  4342. std::unordered_set<std::string> uniqueDirectories;
  4343. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DIRECTORIES", nullptr,
  4344. nullptr);
  4345. cmList debugProperties{ this->Makefile->GetDefinition(
  4346. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  4347. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  4348. cm::contains(debugProperties, "LINK_DIRECTORIES");
  4349. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4350. this->DebugLinkDirectoriesDone = true;
  4351. }
  4352. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  4353. this, config, language, &dagChecker, this->LinkDirectoriesEntries);
  4354. AddInterfaceEntries(this, config, "INTERFACE_LINK_DIRECTORIES", language,
  4355. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4356. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4357. ? LinkInterfaceFor::Link
  4358. : LinkInterfaceFor::Usage);
  4359. processLinkDirectories(this, entries, result, uniqueDirectories,
  4360. debugDirectories);
  4361. this->LinkDirectoriesCache.emplace(cacheKey, result);
  4362. return result;
  4363. }
  4364. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  4365. const std::string& config,
  4366. const std::string& language) const
  4367. {
  4368. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  4369. result.reserve(tmp.size());
  4370. for (BT<std::string>& v : tmp) {
  4371. result.emplace_back(std::move(v.Value));
  4372. }
  4373. }
  4374. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  4375. std::string const& config, std::string const& language) const
  4376. {
  4377. std::vector<BT<std::string>> result;
  4378. std::unordered_set<std::string> uniqueOptions;
  4379. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DEPENDS", nullptr,
  4380. nullptr);
  4381. EvaluatedTargetPropertyEntries entries;
  4382. if (cmValue linkDepends = this->GetProperty("LINK_DEPENDS")) {
  4383. cmList depends{ *linkDepends };
  4384. for (const auto& depend : depends) {
  4385. std::unique_ptr<TargetPropertyEntry> entry = CreateTargetPropertyEntry(
  4386. *this->LocalGenerator->GetCMakeInstance(), depend);
  4387. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  4388. this, config, language, &dagChecker, *entry));
  4389. }
  4390. }
  4391. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS", language,
  4392. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  4393. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  4394. ? LinkInterfaceFor::Link
  4395. : LinkInterfaceFor::Usage);
  4396. processOptions(this, entries, result, uniqueOptions, false, "link depends",
  4397. OptionsParse::None);
  4398. return result;
  4399. }
  4400. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  4401. {
  4402. if (this->IsImported()) {
  4403. return;
  4404. }
  4405. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  4406. // Get the names.
  4407. cmGeneratorTarget::Names targetNames;
  4408. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4409. targetNames = this->GetExecutableNames(config);
  4410. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4411. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4412. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4413. targetNames = this->GetLibraryNames(config);
  4414. } else {
  4415. return;
  4416. }
  4417. // Get the directory.
  4418. std::string dir =
  4419. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  4420. // Add each name.
  4421. std::string f;
  4422. if (!targetNames.Output.empty()) {
  4423. f = cmStrCat(dir, '/', targetNames.Output);
  4424. gg->AddToManifest(f);
  4425. }
  4426. if (!targetNames.SharedObject.empty()) {
  4427. f = cmStrCat(dir, '/', targetNames.SharedObject);
  4428. gg->AddToManifest(f);
  4429. }
  4430. if (!targetNames.Real.empty()) {
  4431. f = cmStrCat(dir, '/', targetNames.Real);
  4432. gg->AddToManifest(f);
  4433. }
  4434. if (!targetNames.PDB.empty()) {
  4435. f = cmStrCat(dir, '/', targetNames.PDB);
  4436. gg->AddToManifest(f);
  4437. }
  4438. dir = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  4439. if (!targetNames.ImportOutput.empty()) {
  4440. f = cmStrCat(dir, '/', targetNames.ImportOutput);
  4441. gg->AddToManifest(f);
  4442. }
  4443. if (!targetNames.ImportLibrary.empty()) {
  4444. f = cmStrCat(dir, '/', targetNames.ImportLibrary);
  4445. gg->AddToManifest(f);
  4446. }
  4447. if (!targetNames.ImportReal.empty()) {
  4448. f = cmStrCat(dir, '/', targetNames.ImportReal);
  4449. gg->AddToManifest(f);
  4450. }
  4451. }
  4452. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  4453. {
  4454. // Compute the language standard based on the compile features.
  4455. cmStandardLevelResolver standardResolver(this->Makefile);
  4456. std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
  4457. for (BT<std::string> const& f : features) {
  4458. std::string lang;
  4459. if (!standardResolver.CompileFeatureKnown(this->Target->GetName(), f.Value,
  4460. lang, nullptr)) {
  4461. return false;
  4462. }
  4463. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  4464. cmValue currentLanguageStandard = this->GetLanguageStandard(lang, config);
  4465. std::string newRequiredStandard;
  4466. if (!standardResolver.GetNewRequiredStandard(
  4467. this->Target->GetName(), f.Value, currentLanguageStandard,
  4468. newRequiredStandard)) {
  4469. return false;
  4470. }
  4471. if (!newRequiredStandard.empty()) {
  4472. BTs<std::string>& languageStandardProperty =
  4473. this->LanguageStandardMap[key];
  4474. if (languageStandardProperty.Value != newRequiredStandard) {
  4475. languageStandardProperty.Value = newRequiredStandard;
  4476. languageStandardProperty.Backtraces.clear();
  4477. }
  4478. languageStandardProperty.Backtraces.emplace_back(f.Backtrace);
  4479. }
  4480. }
  4481. return true;
  4482. }
  4483. bool cmGeneratorTarget::ComputeCompileFeatures(
  4484. std::string const& config, std::set<LanguagePair> const& languagePairs) const
  4485. {
  4486. for (const auto& language : languagePairs) {
  4487. BTs<std::string> const* generatorTargetLanguageStandard =
  4488. this->GetLanguageStandardProperty(language.first, config);
  4489. if (!generatorTargetLanguageStandard) {
  4490. // If the standard isn't explicitly set we copy it over from the
  4491. // specified paired language.
  4492. std::string key =
  4493. cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
  4494. BTs<std::string> const* standardToCopy =
  4495. this->GetLanguageStandardProperty(language.second, config);
  4496. if (standardToCopy) {
  4497. this->LanguageStandardMap[key] = *standardToCopy;
  4498. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4499. } else {
  4500. cmValue defaultStandard = this->Makefile->GetDefinition(
  4501. cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
  4502. if (defaultStandard) {
  4503. this->LanguageStandardMap[key] = BTs<std::string>(*defaultStandard);
  4504. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  4505. }
  4506. }
  4507. // Custom updates for the CUDA standard.
  4508. if (generatorTargetLanguageStandard != nullptr &&
  4509. (language.first == "CUDA")) {
  4510. if (generatorTargetLanguageStandard->Value == "98") {
  4511. this->LanguageStandardMap[key].Value = "03";
  4512. }
  4513. }
  4514. }
  4515. }
  4516. return true;
  4517. }
  4518. std::string cmGeneratorTarget::GetImportedLibName(
  4519. std::string const& config) const
  4520. {
  4521. if (cmGeneratorTarget::ImportInfo const* info =
  4522. this->GetImportInfo(config)) {
  4523. return info->LibName;
  4524. }
  4525. return std::string();
  4526. }
  4527. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  4528. cmStateEnums::ArtifactType artifact,
  4529. bool realname) const
  4530. {
  4531. if (this->IsImported()) {
  4532. return this->Target->ImportedGetFullPath(config, artifact);
  4533. }
  4534. return this->NormalGetFullPath(config, artifact, realname);
  4535. }
  4536. std::string cmGeneratorTarget::NormalGetFullPath(
  4537. const std::string& config, cmStateEnums::ArtifactType artifact,
  4538. bool realname) const
  4539. {
  4540. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  4541. if (this->IsAppBundleOnApple()) {
  4542. fpath =
  4543. cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
  4544. }
  4545. // Add the full name of the target.
  4546. switch (artifact) {
  4547. case cmStateEnums::RuntimeBinaryArtifact:
  4548. if (realname) {
  4549. fpath += this->NormalGetRealName(config);
  4550. } else {
  4551. fpath +=
  4552. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  4553. }
  4554. break;
  4555. case cmStateEnums::ImportLibraryArtifact:
  4556. if (realname) {
  4557. fpath +=
  4558. this->NormalGetRealName(config, cmStateEnums::ImportLibraryArtifact);
  4559. } else {
  4560. fpath +=
  4561. this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  4562. }
  4563. break;
  4564. }
  4565. return fpath;
  4566. }
  4567. std::string cmGeneratorTarget::NormalGetRealName(
  4568. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4569. {
  4570. // This should not be called for imported targets.
  4571. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4572. // enforcement of the limited imported target API.
  4573. if (this->IsImported()) {
  4574. std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
  4575. this->GetName());
  4576. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4577. }
  4578. Names names = this->GetType() == cmStateEnums::EXECUTABLE
  4579. ? this->GetExecutableNames(config)
  4580. : this->GetLibraryNames(config);
  4581. // Compute the real name that will be built.
  4582. return artifact == cmStateEnums::RuntimeBinaryArtifact ? names.Real
  4583. : names.ImportReal;
  4584. }
  4585. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  4586. const std::string& config) const
  4587. {
  4588. cmGeneratorTarget::Names targetNames;
  4589. // This should not be called for imported targets.
  4590. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4591. // enforcement of the limited imported target API.
  4592. if (this->IsImported()) {
  4593. std::string msg =
  4594. cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
  4595. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4596. }
  4597. // Check for library version properties.
  4598. cmValue version = this->GetProperty("VERSION");
  4599. cmValue soversion = this->GetProperty("SOVERSION");
  4600. if (!this->HasSOName(config) ||
  4601. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  4602. this->IsFrameworkOnApple()) {
  4603. // Versioning is supported only for shared libraries and modules,
  4604. // and then only when the platform supports an soname flag.
  4605. version = nullptr;
  4606. soversion = nullptr;
  4607. }
  4608. if (version && !soversion) {
  4609. // The soversion must be set if the library version is set. Use
  4610. // the library version as the soversion.
  4611. soversion = version;
  4612. }
  4613. if (!version && soversion) {
  4614. // Use the soversion as the library version.
  4615. version = soversion;
  4616. }
  4617. // Get the components of the library name.
  4618. NameComponents const& components = this->GetFullNameInternalComponents(
  4619. config, cmStateEnums::RuntimeBinaryArtifact);
  4620. // The library name.
  4621. targetNames.Base = components.base;
  4622. targetNames.Output =
  4623. cmStrCat(components.prefix, targetNames.Base, components.suffix);
  4624. if (this->IsFrameworkOnApple()) {
  4625. targetNames.Real = components.prefix;
  4626. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4627. targetNames.Real +=
  4628. cmStrCat("Versions/", this->GetFrameworkVersion(), '/');
  4629. }
  4630. targetNames.Real += cmStrCat(targetNames.Base, components.suffix);
  4631. targetNames.SharedObject = targetNames.Real;
  4632. } else {
  4633. // The library's soname.
  4634. this->ComputeVersionedName(targetNames.SharedObject, components.prefix,
  4635. targetNames.Base, components.suffix,
  4636. targetNames.Output, soversion);
  4637. // The library's real name on disk.
  4638. this->ComputeVersionedName(targetNames.Real, components.prefix,
  4639. targetNames.Base, components.suffix,
  4640. targetNames.Output, version);
  4641. }
  4642. // The import library names.
  4643. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4644. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4645. NameComponents const& importComponents =
  4646. this->GetFullNameInternalComponents(config,
  4647. cmStateEnums::ImportLibraryArtifact);
  4648. targetNames.ImportOutput = cmStrCat(
  4649. importComponents.prefix, importComponents.base, importComponents.suffix);
  4650. if (this->IsFrameworkOnApple() && this->IsSharedLibraryWithExports()) {
  4651. targetNames.ImportReal = components.prefix;
  4652. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4653. targetNames.ImportReal +=
  4654. cmStrCat("Versions/", this->GetFrameworkVersion(), '/');
  4655. }
  4656. targetNames.ImportReal +=
  4657. cmStrCat(importComponents.base, importComponents.suffix);
  4658. targetNames.ImportLibrary = targetNames.ImportOutput;
  4659. } else {
  4660. // The import library's soname.
  4661. this->ComputeVersionedName(
  4662. targetNames.ImportLibrary, importComponents.prefix,
  4663. importComponents.base, importComponents.suffix,
  4664. targetNames.ImportOutput, soversion);
  4665. // The import library's real name on disk.
  4666. this->ComputeVersionedName(
  4667. targetNames.ImportReal, importComponents.prefix, importComponents.base,
  4668. importComponents.suffix, targetNames.ImportOutput, version);
  4669. }
  4670. }
  4671. // The program database file name.
  4672. targetNames.PDB = this->GetPDBName(config);
  4673. return targetNames;
  4674. }
  4675. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  4676. const std::string& config) const
  4677. {
  4678. cmGeneratorTarget::Names targetNames;
  4679. // This should not be called for imported targets.
  4680. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4681. // enforcement of the limited imported target API.
  4682. if (this->IsImported()) {
  4683. std::string msg = cmStrCat(
  4684. "GetExecutableNames called on imported target: ", this->GetName());
  4685. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4686. }
  4687. // This versioning is supported only for executables and then only
  4688. // when the platform supports symbolic links.
  4689. #if defined(_WIN32) && !defined(__CYGWIN__)
  4690. cmValue version;
  4691. #else
  4692. // Check for executable version properties.
  4693. cmValue version = this->GetProperty("VERSION");
  4694. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  4695. this->Makefile->IsOn("XCODE")) {
  4696. version = nullptr;
  4697. }
  4698. #endif
  4699. // Get the components of the executable name.
  4700. NameComponents const& components = this->GetFullNameInternalComponents(
  4701. config, cmStateEnums::RuntimeBinaryArtifact);
  4702. // The executable name.
  4703. targetNames.Base = components.base;
  4704. targetNames.Output =
  4705. components.prefix + targetNames.Base + components.suffix;
  4706. // The executable's real name on disk.
  4707. #if defined(__CYGWIN__)
  4708. targetNames.Real = components.prefix + targetNames.Base;
  4709. #else
  4710. targetNames.Real = targetNames.Output;
  4711. #endif
  4712. if (version) {
  4713. targetNames.Real += "-";
  4714. targetNames.Real += *version;
  4715. }
  4716. #if defined(__CYGWIN__)
  4717. targetNames.Real += components.suffix;
  4718. #endif
  4719. // The import library name.
  4720. targetNames.ImportLibrary =
  4721. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4722. targetNames.ImportReal = targetNames.ImportLibrary;
  4723. targetNames.ImportOutput = targetNames.ImportLibrary;
  4724. // The program database file name.
  4725. targetNames.PDB = this->GetPDBName(config);
  4726. return targetNames;
  4727. }
  4728. std::string cmGeneratorTarget::GetFullNameInternal(
  4729. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4730. {
  4731. NameComponents const& components =
  4732. this->GetFullNameInternalComponents(config, artifact);
  4733. return components.prefix + components.base + components.suffix;
  4734. }
  4735. std::string cmGeneratorTarget::ImportedGetLocation(
  4736. const std::string& config) const
  4737. {
  4738. assert(this->IsImported());
  4739. return this->Target->ImportedGetFullPath(
  4740. config, cmStateEnums::RuntimeBinaryArtifact);
  4741. }
  4742. std::string cmGeneratorTarget::GetFullNameImported(
  4743. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4744. {
  4745. return cmSystemTools::GetFilenameName(
  4746. this->Target->ImportedGetFullPath(config, artifact));
  4747. }
  4748. cmGeneratorTarget::NameComponents const&
  4749. cmGeneratorTarget::GetFullNameInternalComponents(
  4750. std::string const& config, cmStateEnums::ArtifactType artifact) const
  4751. {
  4752. assert(artifact == cmStateEnums::RuntimeBinaryArtifact ||
  4753. artifact == cmStateEnums::ImportLibraryArtifact);
  4754. FullNameCache& cache = artifact == cmStateEnums::RuntimeBinaryArtifact
  4755. ? RuntimeBinaryFullNameCache
  4756. : ImportLibraryFullNameCache;
  4757. auto search = cache.find(config);
  4758. if (search != cache.end()) {
  4759. return search->second;
  4760. }
  4761. // Use just the target name for non-main target types.
  4762. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  4763. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4764. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4765. this->GetType() != cmStateEnums::EXECUTABLE) {
  4766. NameComponents components;
  4767. components.base = this->GetName();
  4768. return cache.emplace(config, std::move(components)).first->second;
  4769. }
  4770. const bool isImportedLibraryArtifact =
  4771. (artifact == cmStateEnums::ImportLibraryArtifact);
  4772. // Return an empty name for the import library if this platform
  4773. // does not support import libraries.
  4774. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  4775. return cache.emplace(config, NameComponents()).first->second;
  4776. }
  4777. NameComponents parts;
  4778. std::string& outPrefix = parts.prefix;
  4779. std::string& outBase = parts.base;
  4780. std::string& outSuffix = parts.suffix;
  4781. // retrieve prefix and suffix
  4782. std::string ll = this->GetLinkerLanguage(config);
  4783. cmValue targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
  4784. cmValue targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
  4785. // The implib option is only allowed for shared libraries, module
  4786. // libraries, and executables.
  4787. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4788. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4789. this->GetType() != cmStateEnums::EXECUTABLE) {
  4790. artifact = cmStateEnums::RuntimeBinaryArtifact;
  4791. }
  4792. // Compute the full name for main target types.
  4793. std::string configPostfix = this->GetFilePostfix(config);
  4794. // frameworks have directory prefix
  4795. std::string fw_prefix;
  4796. if (this->IsFrameworkOnApple()) {
  4797. fw_prefix =
  4798. cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
  4799. targetPrefix = cmValue(fw_prefix);
  4800. if (!isImportedLibraryArtifact) {
  4801. // no suffix
  4802. targetSuffix = nullptr;
  4803. }
  4804. }
  4805. if (this->IsCFBundleOnApple()) {
  4806. fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
  4807. targetPrefix = cmValue(fw_prefix);
  4808. targetSuffix = nullptr;
  4809. }
  4810. // Begin the final name with the prefix.
  4811. outPrefix = targetPrefix ? *targetPrefix : "";
  4812. // Append the target name or property-specified name.
  4813. outBase += this->GetOutputName(config, artifact);
  4814. // Append the per-configuration postfix.
  4815. // When using Xcode, the postfix should be part of the suffix rather than
  4816. // the base, because the suffix ends up being used in Xcode's
  4817. // EXECUTABLE_SUFFIX attribute.
  4818. if (this->IsFrameworkOnApple() && this->GetGlobalGenerator()->IsXcode()) {
  4819. configPostfix += *targetSuffix;
  4820. targetSuffix = cmValue(configPostfix);
  4821. } else {
  4822. outBase += configPostfix;
  4823. }
  4824. // Name shared libraries with their version number on some platforms.
  4825. if (cmValue soversion = this->GetProperty("SOVERSION")) {
  4826. cmValue dllProp;
  4827. if (this->IsDLLPlatform()) {
  4828. dllProp = this->GetProperty("DLL_NAME_WITH_SOVERSION");
  4829. }
  4830. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  4831. !isImportedLibraryArtifact &&
  4832. (dllProp.IsOn() ||
  4833. (!dllProp.IsSet() &&
  4834. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")))) {
  4835. outBase += "-";
  4836. outBase += *soversion;
  4837. }
  4838. }
  4839. // Append the suffix.
  4840. outSuffix = targetSuffix ? *targetSuffix : "";
  4841. return cache.emplace(config, std::move(parts)).first->second;
  4842. }
  4843. std::string cmGeneratorTarget::GetLinkerLanguage(
  4844. const std::string& config) const
  4845. {
  4846. return this->GetLinkClosure(config)->LinkerLanguage;
  4847. }
  4848. std::string cmGeneratorTarget::GetPDBOutputName(
  4849. const std::string& config) const
  4850. {
  4851. std::string base =
  4852. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  4853. std::vector<std::string> props;
  4854. std::string configUpper = cmSystemTools::UpperCase(config);
  4855. if (!configUpper.empty()) {
  4856. // PDB_NAME_<CONFIG>
  4857. props.push_back("PDB_NAME_" + configUpper);
  4858. }
  4859. // PDB_NAME
  4860. props.emplace_back("PDB_NAME");
  4861. for (std::string const& p : props) {
  4862. if (cmValue outName = this->GetProperty(p)) {
  4863. base = *outName;
  4864. break;
  4865. }
  4866. }
  4867. return base;
  4868. }
  4869. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  4870. {
  4871. NameComponents const& parts = this->GetFullNameInternalComponents(
  4872. config, cmStateEnums::RuntimeBinaryArtifact);
  4873. std::vector<std::string> props;
  4874. std::string configUpper = cmSystemTools::UpperCase(config);
  4875. if (!configUpper.empty()) {
  4876. // PDB_NAME_<CONFIG>
  4877. props.push_back("PDB_NAME_" + configUpper);
  4878. }
  4879. // PDB_NAME
  4880. props.emplace_back("PDB_NAME");
  4881. for (std::string const& p : props) {
  4882. if (cmValue outName = this->GetProperty(p)) {
  4883. return parts.prefix + *outName + ".pdb";
  4884. }
  4885. }
  4886. return parts.prefix + parts.base + ".pdb";
  4887. }
  4888. std::string cmGeneratorTarget::GetObjectDirectory(
  4889. std::string const& config) const
  4890. {
  4891. std::string obj_dir =
  4892. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  4893. #if defined(__APPLE__)
  4894. // Replace Xcode's placeholder for the object file directory since
  4895. // installation and export scripts need to know the real directory.
  4896. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  4897. // but we use the default here. Users that want to enable sanitizers
  4898. // will do so at the cost of object library installation and export.
  4899. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  4900. "Objects-normal");
  4901. #endif
  4902. return obj_dir;
  4903. }
  4904. void cmGeneratorTarget::GetTargetObjectNames(
  4905. std::string const& config, std::vector<std::string>& objects) const
  4906. {
  4907. std::vector<cmSourceFile const*> objectSources;
  4908. this->GetObjectSources(objectSources, config);
  4909. std::map<cmSourceFile const*, std::string> mapping;
  4910. for (cmSourceFile const* sf : objectSources) {
  4911. mapping[sf];
  4912. }
  4913. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  4914. for (cmSourceFile const* src : objectSources) {
  4915. // Find the object file name corresponding to this source file.
  4916. auto map_it = mapping.find(src);
  4917. // It must exist because we populated the mapping just above.
  4918. assert(!map_it->second.empty());
  4919. objects.push_back(map_it->second);
  4920. }
  4921. // We need to compute the relative path from the root of
  4922. // of the object directory to handle subdirectory paths
  4923. std::string rootObjectDir = this->GetObjectDirectory(config);
  4924. rootObjectDir = cmSystemTools::CollapseFullPath(rootObjectDir);
  4925. auto ispcObjects = this->GetGeneratedISPCObjects(config);
  4926. for (std::string const& output : ispcObjects) {
  4927. auto relativePathFromObjectDir = output.substr(rootObjectDir.size());
  4928. objects.push_back(relativePathFromObjectDir);
  4929. }
  4930. }
  4931. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  4932. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  4933. {
  4934. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  4935. if (nameResult == 0) {
  4936. return strcmp(
  4937. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  4938. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  4939. }
  4940. return nameResult < 0;
  4941. }
  4942. struct cmGeneratorTarget::SourceFileFlags
  4943. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  4944. {
  4945. struct SourceFileFlags flags;
  4946. this->ConstructSourceFileFlags();
  4947. auto si = this->SourceFlagsMap.find(sf);
  4948. if (si != this->SourceFlagsMap.end()) {
  4949. flags = si->second;
  4950. } else {
  4951. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  4952. // were not listed in one of the other lists.
  4953. if (cmValue location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  4954. flags.MacFolder = location->c_str();
  4955. const bool stripResources =
  4956. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  4957. if (*location == "Resources") {
  4958. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4959. if (stripResources) {
  4960. flags.MacFolder = "";
  4961. }
  4962. } else if (cmHasLiteralPrefix(*location, "Resources/")) {
  4963. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  4964. if (stripResources) {
  4965. flags.MacFolder += cmStrLen("Resources/");
  4966. }
  4967. } else {
  4968. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  4969. }
  4970. }
  4971. }
  4972. return flags;
  4973. }
  4974. void cmGeneratorTarget::ConstructSourceFileFlags() const
  4975. {
  4976. if (this->SourceFileFlagsConstructed) {
  4977. return;
  4978. }
  4979. this->SourceFileFlagsConstructed = true;
  4980. // Process public headers to mark the source files.
  4981. if (cmValue files = this->GetProperty("PUBLIC_HEADER")) {
  4982. cmList relFiles{ *files };
  4983. for (auto const& relFile : relFiles) {
  4984. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4985. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4986. flags.MacFolder = "Headers";
  4987. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  4988. }
  4989. }
  4990. }
  4991. // Process private headers after public headers so that they take
  4992. // precedence if a file is listed in both.
  4993. if (cmValue files = this->GetProperty("PRIVATE_HEADER")) {
  4994. cmList relFiles{ *files };
  4995. for (auto const& relFile : relFiles) {
  4996. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4997. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4998. flags.MacFolder = "PrivateHeaders";
  4999. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  5000. }
  5001. }
  5002. }
  5003. // Mark sources listed as resources.
  5004. if (cmValue files = this->GetProperty("RESOURCE")) {
  5005. cmList relFiles{ *files };
  5006. for (auto const& relFile : relFiles) {
  5007. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  5008. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  5009. flags.MacFolder = "";
  5010. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  5011. flags.MacFolder = "Resources";
  5012. }
  5013. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  5014. }
  5015. }
  5016. }
  5017. }
  5018. const cmGeneratorTarget::CompatibleInterfacesBase&
  5019. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  5020. {
  5021. cmGeneratorTarget::CompatibleInterfaces& compat =
  5022. this->CompatibleInterfacesMap[config];
  5023. if (!compat.Done) {
  5024. compat.Done = true;
  5025. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  5026. compat.PropsString.insert("AUTOUIC_OPTIONS");
  5027. std::vector<cmGeneratorTarget const*> const& deps =
  5028. this->GetLinkImplementationClosure(config);
  5029. for (cmGeneratorTarget const* li : deps) {
  5030. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  5031. if (cmValue prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  5032. cmList props(*prop); \
  5033. compat.Props##x.insert(props.begin(), props.end()); \
  5034. }
  5035. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  5036. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  5037. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  5038. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  5039. #undef CM_READ_COMPATIBLE_INTERFACE
  5040. }
  5041. }
  5042. return compat;
  5043. }
  5044. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  5045. const std::string& p, const std::string& config) const
  5046. {
  5047. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5048. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5049. return false;
  5050. }
  5051. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  5052. }
  5053. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  5054. const std::string& p, const std::string& config) const
  5055. {
  5056. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5057. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5058. return false;
  5059. }
  5060. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  5061. }
  5062. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  5063. const std::string& p, const std::string& config) const
  5064. {
  5065. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5066. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5067. return false;
  5068. }
  5069. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  5070. }
  5071. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  5072. const std::string& p, const std::string& config) const
  5073. {
  5074. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5075. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5076. return false;
  5077. }
  5078. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  5079. }
  5080. enum CompatibleType
  5081. {
  5082. BoolType,
  5083. StringType,
  5084. NumberMinType,
  5085. NumberMaxType
  5086. };
  5087. template <typename PropertyType>
  5088. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5089. const std::string& prop,
  5090. const std::string& config,
  5091. CompatibleType, PropertyType*);
  5092. template <>
  5093. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5094. const std::string& prop,
  5095. const std::string& config,
  5096. CompatibleType /*unused*/,
  5097. bool* /*unused*/)
  5098. {
  5099. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  5100. }
  5101. template <>
  5102. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  5103. const std::string& prop,
  5104. const std::string& config,
  5105. CompatibleType t,
  5106. const char** /*unused*/)
  5107. {
  5108. switch (t) {
  5109. case BoolType:
  5110. assert(false &&
  5111. "String compatibility check function called for boolean");
  5112. return nullptr;
  5113. case StringType:
  5114. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  5115. case NumberMinType:
  5116. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  5117. case NumberMaxType:
  5118. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  5119. }
  5120. assert(false && "Unreachable!");
  5121. return nullptr;
  5122. }
  5123. template <typename PropertyType>
  5124. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  5125. cmGeneratorTarget const* dependee,
  5126. const std::string& propName,
  5127. std::set<std::string>& emitted,
  5128. const std::string& config, CompatibleType t,
  5129. PropertyType* /*unused*/)
  5130. {
  5131. cmValue prop = dependee->GetProperty(propName);
  5132. if (!prop) {
  5133. return;
  5134. }
  5135. cmList props{ *prop };
  5136. std::string pdir =
  5137. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Help/prop_tgt/");
  5138. for (std::string const& p : props) {
  5139. std::string pname = cmSystemTools::HelpFileName(p);
  5140. std::string pfile = pdir + pname + ".rst";
  5141. if (cmSystemTools::FileExists(pfile, true)) {
  5142. std::ostringstream e;
  5143. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  5144. << "\" listed in its " << propName
  5145. << " property. "
  5146. "This is not allowed. Only user-defined properties may appear "
  5147. "listed in the "
  5148. << propName << " property.";
  5149. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  5150. e.str());
  5151. return;
  5152. }
  5153. if (emitted.insert(p).second) {
  5154. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  5155. nullptr);
  5156. if (cmSystemTools::GetErrorOccurredFlag()) {
  5157. return;
  5158. }
  5159. }
  5160. }
  5161. }
  5162. namespace {
  5163. std::string intersect(const std::set<std::string>& s1,
  5164. const std::set<std::string>& s2)
  5165. {
  5166. std::set<std::string> intersect;
  5167. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  5168. std::inserter(intersect, intersect.begin()));
  5169. if (!intersect.empty()) {
  5170. return *intersect.begin();
  5171. }
  5172. return "";
  5173. }
  5174. std::string intersect(const std::set<std::string>& s1,
  5175. const std::set<std::string>& s2,
  5176. const std::set<std::string>& s3)
  5177. {
  5178. std::string result;
  5179. result = intersect(s1, s2);
  5180. if (!result.empty()) {
  5181. return result;
  5182. }
  5183. result = intersect(s1, s3);
  5184. if (!result.empty()) {
  5185. return result;
  5186. }
  5187. return intersect(s2, s3);
  5188. }
  5189. std::string intersect(const std::set<std::string>& s1,
  5190. const std::set<std::string>& s2,
  5191. const std::set<std::string>& s3,
  5192. const std::set<std::string>& s4)
  5193. {
  5194. std::string result;
  5195. result = intersect(s1, s2);
  5196. if (!result.empty()) {
  5197. return result;
  5198. }
  5199. result = intersect(s1, s3);
  5200. if (!result.empty()) {
  5201. return result;
  5202. }
  5203. result = intersect(s1, s4);
  5204. if (!result.empty()) {
  5205. return result;
  5206. }
  5207. return intersect(s2, s3, s4);
  5208. }
  5209. }
  5210. void cmGeneratorTarget::CheckPropertyCompatibility(
  5211. cmComputeLinkInformation& info, const std::string& config) const
  5212. {
  5213. const cmComputeLinkInformation::ItemVector& deps = info.GetItems();
  5214. std::set<std::string> emittedBools;
  5215. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  5216. std::set<std::string> emittedStrings;
  5217. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  5218. std::set<std::string> emittedMinNumbers;
  5219. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  5220. std::set<std::string> emittedMaxNumbers;
  5221. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  5222. for (auto const& dep : deps) {
  5223. if (!dep.Target || dep.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  5224. continue;
  5225. }
  5226. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  5227. config, BoolType, nullptr);
  5228. if (cmSystemTools::GetErrorOccurredFlag()) {
  5229. return;
  5230. }
  5231. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  5232. emittedStrings, config, StringType,
  5233. nullptr);
  5234. if (cmSystemTools::GetErrorOccurredFlag()) {
  5235. return;
  5236. }
  5237. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  5238. emittedMinNumbers, config,
  5239. NumberMinType, nullptr);
  5240. if (cmSystemTools::GetErrorOccurredFlag()) {
  5241. return;
  5242. }
  5243. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  5244. emittedMaxNumbers, config,
  5245. NumberMaxType, nullptr);
  5246. if (cmSystemTools::GetErrorOccurredFlag()) {
  5247. return;
  5248. }
  5249. }
  5250. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  5251. emittedMaxNumbers);
  5252. if (!prop.empty()) {
  5253. // Use a sorted std::vector to keep the error message sorted.
  5254. std::vector<std::string> props;
  5255. auto i = emittedBools.find(prop);
  5256. if (i != emittedBools.end()) {
  5257. props.push_back(strBool);
  5258. }
  5259. i = emittedStrings.find(prop);
  5260. if (i != emittedStrings.end()) {
  5261. props.push_back(strString);
  5262. }
  5263. i = emittedMinNumbers.find(prop);
  5264. if (i != emittedMinNumbers.end()) {
  5265. props.push_back(strNumMin);
  5266. }
  5267. i = emittedMaxNumbers.find(prop);
  5268. if (i != emittedMaxNumbers.end()) {
  5269. props.push_back(strNumMax);
  5270. }
  5271. std::sort(props.begin(), props.end());
  5272. std::string propsString = cmStrCat(
  5273. cmJoin(cmMakeRange(props).retreat(1), ", "), " and the ", props.back());
  5274. std::ostringstream e;
  5275. e << "Property \"" << prop << "\" appears in both the " << propsString
  5276. << " property in the dependencies of target \"" << this->GetName()
  5277. << "\". This is not allowed. A property may only require "
  5278. "compatibility "
  5279. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  5280. "or a "
  5281. "string interpretation, but not a mixture.";
  5282. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  5283. }
  5284. }
  5285. template <typename PropertyType>
  5286. std::string valueAsString(PropertyType);
  5287. template <>
  5288. std::string valueAsString<bool>(bool value)
  5289. {
  5290. return value ? "TRUE" : "FALSE";
  5291. }
  5292. template <>
  5293. std::string valueAsString<const char*>(const char* value)
  5294. {
  5295. return value ? value : "(unset)";
  5296. }
  5297. template <>
  5298. std::string valueAsString<std::string>(std::string value)
  5299. {
  5300. return value;
  5301. }
  5302. template <>
  5303. std::string valueAsString<cmValue>(cmValue value)
  5304. {
  5305. return value ? *value : std::string("(unset)");
  5306. }
  5307. template <>
  5308. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  5309. {
  5310. return "(unset)";
  5311. }
  5312. static std::string compatibilityType(CompatibleType t)
  5313. {
  5314. switch (t) {
  5315. case BoolType:
  5316. return "Boolean compatibility";
  5317. case StringType:
  5318. return "String compatibility";
  5319. case NumberMaxType:
  5320. return "Numeric maximum compatibility";
  5321. case NumberMinType:
  5322. return "Numeric minimum compatibility";
  5323. }
  5324. assert(false && "Unreachable!");
  5325. return "";
  5326. }
  5327. static std::string compatibilityAgree(CompatibleType t, bool dominant)
  5328. {
  5329. switch (t) {
  5330. case BoolType:
  5331. case StringType:
  5332. return dominant ? "(Disagree)\n" : "(Agree)\n";
  5333. case NumberMaxType:
  5334. case NumberMinType:
  5335. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  5336. }
  5337. assert(false && "Unreachable!");
  5338. return "";
  5339. }
  5340. template <typename PropertyType>
  5341. PropertyType getTypedProperty(
  5342. cmGeneratorTarget const* tgt, const std::string& prop,
  5343. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  5344. template <>
  5345. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  5346. const std::string& prop,
  5347. cmGeneratorExpressionInterpreter* genexInterpreter)
  5348. {
  5349. if (genexInterpreter == nullptr) {
  5350. return tgt->GetPropertyAsBool(prop);
  5351. }
  5352. cmValue value = tgt->GetProperty(prop);
  5353. return cmIsOn(genexInterpreter->Evaluate(value ? *value : "", prop));
  5354. }
  5355. template <>
  5356. const char* getTypedProperty<const char*>(
  5357. cmGeneratorTarget const* tgt, const std::string& prop,
  5358. cmGeneratorExpressionInterpreter* genexInterpreter)
  5359. {
  5360. cmValue value = tgt->GetProperty(prop);
  5361. if (genexInterpreter == nullptr) {
  5362. return value.GetCStr();
  5363. }
  5364. return genexInterpreter->Evaluate(value ? *value : "", prop).c_str();
  5365. }
  5366. template <>
  5367. std::string getTypedProperty<std::string>(
  5368. cmGeneratorTarget const* tgt, const std::string& prop,
  5369. cmGeneratorExpressionInterpreter* genexInterpreter)
  5370. {
  5371. cmValue value = tgt->GetProperty(prop);
  5372. if (genexInterpreter == nullptr) {
  5373. return valueAsString(value);
  5374. }
  5375. return genexInterpreter->Evaluate(value ? *value : "", prop);
  5376. }
  5377. template <typename PropertyType>
  5378. PropertyType impliedValue(PropertyType);
  5379. template <>
  5380. bool impliedValue<bool>(bool /*unused*/)
  5381. {
  5382. return false;
  5383. }
  5384. template <>
  5385. const char* impliedValue<const char*>(const char* /*unused*/)
  5386. {
  5387. return "";
  5388. }
  5389. template <>
  5390. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  5391. {
  5392. return std::string();
  5393. }
  5394. template <typename PropertyType>
  5395. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  5396. PropertyType rhs,
  5397. CompatibleType t);
  5398. template <>
  5399. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  5400. CompatibleType /*unused*/)
  5401. {
  5402. return { lhs == rhs, lhs };
  5403. }
  5404. static std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  5405. const char* rhs)
  5406. {
  5407. const bool b = strcmp(lhs, rhs) == 0;
  5408. return { b, b ? lhs : nullptr };
  5409. }
  5410. static std::pair<bool, std::string> consistentStringProperty(
  5411. const std::string& lhs, const std::string& rhs)
  5412. {
  5413. const bool b = lhs == rhs;
  5414. return { b, b ? lhs : valueAsString(nullptr) };
  5415. }
  5416. static std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  5417. const char* rhs,
  5418. CompatibleType t)
  5419. {
  5420. char* pEnd;
  5421. long lnum = strtol(lhs, &pEnd, 0);
  5422. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  5423. return { false, nullptr };
  5424. }
  5425. long rnum = strtol(rhs, &pEnd, 0);
  5426. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  5427. return { false, nullptr };
  5428. }
  5429. if (t == NumberMaxType) {
  5430. return { true, std::max(lnum, rnum) == lnum ? lhs : rhs };
  5431. }
  5432. return { true, std::min(lnum, rnum) == lnum ? lhs : rhs };
  5433. }
  5434. template <>
  5435. std::pair<bool, const char*> consistentProperty(const char* lhs,
  5436. const char* rhs,
  5437. CompatibleType t)
  5438. {
  5439. if (!lhs && !rhs) {
  5440. return { true, lhs };
  5441. }
  5442. if (!lhs) {
  5443. return { true, rhs };
  5444. }
  5445. if (!rhs) {
  5446. return { true, lhs };
  5447. }
  5448. switch (t) {
  5449. case BoolType: {
  5450. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5451. return { same, same ? lhs : nullptr };
  5452. }
  5453. case StringType:
  5454. return consistentStringProperty(lhs, rhs);
  5455. case NumberMinType:
  5456. case NumberMaxType:
  5457. return consistentNumberProperty(lhs, rhs, t);
  5458. }
  5459. assert(false && "Unreachable!");
  5460. return { false, nullptr };
  5461. }
  5462. static std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  5463. const std::string& rhs,
  5464. CompatibleType t)
  5465. {
  5466. const std::string null_ptr = valueAsString(nullptr);
  5467. if (lhs == null_ptr && rhs == null_ptr) {
  5468. return { true, lhs };
  5469. }
  5470. if (lhs == null_ptr) {
  5471. return { true, rhs };
  5472. }
  5473. if (rhs == null_ptr) {
  5474. return { true, lhs };
  5475. }
  5476. switch (t) {
  5477. case BoolType: {
  5478. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  5479. return { same, same ? lhs : null_ptr };
  5480. }
  5481. case StringType:
  5482. return consistentStringProperty(lhs, rhs);
  5483. case NumberMinType:
  5484. case NumberMaxType: {
  5485. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  5486. return { value.first,
  5487. value.first ? std::string(value.second) : null_ptr };
  5488. }
  5489. }
  5490. assert(false && "Unreachable!");
  5491. return { false, null_ptr };
  5492. }
  5493. template <typename PropertyType>
  5494. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  5495. const std::string& p,
  5496. const std::string& config,
  5497. const char* defaultValue,
  5498. CompatibleType t,
  5499. PropertyType* /*unused*/)
  5500. {
  5501. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  5502. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  5503. const bool explicitlySet = cm::contains(headPropKeys, p);
  5504. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  5505. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  5506. std::vector<cmGeneratorTarget const*> const& deps =
  5507. tgt->GetLinkImplementationClosure(config);
  5508. if (deps.empty()) {
  5509. return propContent;
  5510. }
  5511. bool propInitialized = explicitlySet;
  5512. std::string report = cmStrCat(" * Target \"", tgt->GetName());
  5513. if (explicitlySet) {
  5514. report += "\" has property content \"";
  5515. report += valueAsString<PropertyType>(propContent);
  5516. report += "\"\n";
  5517. } else if (impliedByUse) {
  5518. report += "\" property is implied by use.\n";
  5519. } else {
  5520. report += "\" property not set.\n";
  5521. }
  5522. std::string interfaceProperty = "INTERFACE_" + p;
  5523. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter;
  5524. if (p == "POSITION_INDEPENDENT_CODE") {
  5525. genexInterpreter = cm::make_unique<cmGeneratorExpressionInterpreter>(
  5526. tgt->GetLocalGenerator(), config, tgt);
  5527. }
  5528. for (cmGeneratorTarget const* theTarget : deps) {
  5529. // An error should be reported if one dependency
  5530. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  5531. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  5532. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  5533. // with a dependency.
  5534. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  5535. const bool ifaceIsSet = cm::contains(propKeys, interfaceProperty);
  5536. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  5537. theTarget, interfaceProperty, genexInterpreter.get());
  5538. std::string reportEntry;
  5539. if (ifaceIsSet) {
  5540. reportEntry += " * Target \"";
  5541. reportEntry += theTarget->GetName();
  5542. reportEntry += "\" property value \"";
  5543. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  5544. reportEntry += "\" ";
  5545. }
  5546. if (explicitlySet) {
  5547. if (ifaceIsSet) {
  5548. std::pair<bool, PropertyType> consistent =
  5549. consistentProperty(propContent, ifacePropContent, t);
  5550. report += reportEntry;
  5551. report += compatibilityAgree(t, propContent != consistent.second);
  5552. if (!consistent.first) {
  5553. std::ostringstream e;
  5554. e << "Property " << p << " on target \"" << tgt->GetName()
  5555. << "\" does\nnot match the "
  5556. "INTERFACE_"
  5557. << p
  5558. << " property requirement\nof "
  5559. "dependency \""
  5560. << theTarget->GetName() << "\".\n";
  5561. cmSystemTools::Error(e.str());
  5562. break;
  5563. }
  5564. propContent = consistent.second;
  5565. continue;
  5566. }
  5567. // Explicitly set on target and not set in iface. Can't disagree.
  5568. continue;
  5569. }
  5570. if (impliedByUse) {
  5571. propContent = impliedValue<PropertyType>(propContent);
  5572. if (ifaceIsSet) {
  5573. std::pair<bool, PropertyType> consistent =
  5574. consistentProperty(propContent, ifacePropContent, t);
  5575. report += reportEntry;
  5576. report += compatibilityAgree(t, propContent != consistent.second);
  5577. if (!consistent.first) {
  5578. std::ostringstream e;
  5579. e << "Property " << p << " on target \"" << tgt->GetName()
  5580. << "\" is\nimplied to be " << defaultValue
  5581. << " because it was used to determine the link libraries\n"
  5582. "already. The INTERFACE_"
  5583. << p << " property on\ndependency \"" << theTarget->GetName()
  5584. << "\" is in conflict.\n";
  5585. cmSystemTools::Error(e.str());
  5586. break;
  5587. }
  5588. propContent = consistent.second;
  5589. continue;
  5590. }
  5591. // Implicitly set on target and not set in iface. Can't disagree.
  5592. continue;
  5593. }
  5594. if (ifaceIsSet) {
  5595. if (propInitialized) {
  5596. std::pair<bool, PropertyType> consistent =
  5597. consistentProperty(propContent, ifacePropContent, t);
  5598. report += reportEntry;
  5599. report += compatibilityAgree(t, propContent != consistent.second);
  5600. if (!consistent.first) {
  5601. std::ostringstream e;
  5602. e << "The INTERFACE_" << p << " property of \""
  5603. << theTarget->GetName() << "\" does\nnot agree with the value of "
  5604. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  5605. cmSystemTools::Error(e.str());
  5606. break;
  5607. }
  5608. propContent = consistent.second;
  5609. continue;
  5610. }
  5611. report += reportEntry + "(Interface set)\n";
  5612. propContent = ifacePropContent;
  5613. propInitialized = true;
  5614. } else {
  5615. // Not set. Nothing to agree on.
  5616. continue;
  5617. }
  5618. }
  5619. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  5620. report, compatibilityType(t));
  5621. return propContent;
  5622. }
  5623. bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
  5624. {
  5625. bool previous = this->DeviceLink;
  5626. this->DeviceLink = deviceLink;
  5627. return previous;
  5628. }
  5629. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  5630. const std::string& p, const std::string& config) const
  5631. {
  5632. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  5633. BoolType, nullptr);
  5634. }
  5635. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  5636. const std::string& p, const std::string& config) const
  5637. {
  5638. return checkInterfacePropertyCompatibility<std::string>(
  5639. this, p, config, "FALSE", BoolType, nullptr);
  5640. }
  5641. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  5642. const std::string& p, const std::string& config) const
  5643. {
  5644. return checkInterfacePropertyCompatibility<const char*>(
  5645. this, p, config, "empty", StringType, nullptr);
  5646. }
  5647. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  5648. const std::string& p, const std::string& config) const
  5649. {
  5650. return checkInterfacePropertyCompatibility<const char*>(
  5651. this, p, config, "empty", NumberMinType, nullptr);
  5652. }
  5653. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  5654. const std::string& p, const std::string& config) const
  5655. {
  5656. return checkInterfacePropertyCompatibility<const char*>(
  5657. this, p, config, "empty", NumberMaxType, nullptr);
  5658. }
  5659. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  5660. const std::string& config) const
  5661. {
  5662. // Lookup any existing information for this configuration.
  5663. std::string key(cmSystemTools::UpperCase(config));
  5664. auto i = this->LinkInformation.find(key);
  5665. if (i == this->LinkInformation.end()) {
  5666. // Compute information for this configuration.
  5667. auto info = cm::make_unique<cmComputeLinkInformation>(this, config);
  5668. if (info && !info->Compute()) {
  5669. info.reset();
  5670. }
  5671. // Store the information for this configuration.
  5672. i = this->LinkInformation.emplace(key, std::move(info)).first;
  5673. if (i->second) {
  5674. this->CheckPropertyCompatibility(*i->second, config);
  5675. }
  5676. }
  5677. return i->second.get();
  5678. }
  5679. void cmGeneratorTarget::CheckLinkLibraries() const
  5680. {
  5681. bool linkLibrariesOnlyTargets =
  5682. this->GetPropertyAsBool("LINK_LIBRARIES_ONLY_TARGETS");
  5683. // Evaluate the link interface of this target if needed for extra checks.
  5684. if (linkLibrariesOnlyTargets) {
  5685. std::vector<std::string> const& configs =
  5686. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  5687. for (std::string const& config : configs) {
  5688. this->GetLinkInterfaceLibraries(config, this, LinkInterfaceFor::Link);
  5689. }
  5690. }
  5691. // Check link the implementation for each generated configuration.
  5692. for (auto const& hmp : this->LinkImplMap) {
  5693. HeadToLinkImplementationMap const& hm = hmp.second;
  5694. // There could be several entries used when computing the pre-CMP0022
  5695. // default link interface. Check only the entry for our own link impl.
  5696. auto const hmi = hm.find(this);
  5697. if (hmi == hm.end() || !hmi->second.LibrariesDone) {
  5698. continue;
  5699. }
  5700. for (cmLinkImplItem const& item : hmi->second.Libraries) {
  5701. if (!this->VerifyLinkItemColons(LinkItemRole::Implementation, item)) {
  5702. return;
  5703. }
  5704. if (linkLibrariesOnlyTargets &&
  5705. !this->VerifyLinkItemIsTarget(LinkItemRole::Implementation, item)) {
  5706. return;
  5707. }
  5708. }
  5709. }
  5710. // Check link the interface for each generated combination of
  5711. // configuration and consuming head target. We should not need to
  5712. // consider LinkInterfaceUsageRequirementsOnlyMap because its entries
  5713. // should be a subset of LinkInterfaceMap (with LINK_ONLY left out).
  5714. for (auto const& hmp : this->LinkInterfaceMap) {
  5715. for (auto const& hmi : hmp.second) {
  5716. if (!hmi.second.LibrariesDone) {
  5717. continue;
  5718. }
  5719. for (cmLinkItem const& item : hmi.second.Libraries) {
  5720. if (!this->VerifyLinkItemColons(LinkItemRole::Interface, item)) {
  5721. return;
  5722. }
  5723. if (linkLibrariesOnlyTargets &&
  5724. !this->VerifyLinkItemIsTarget(LinkItemRole::Interface, item)) {
  5725. return;
  5726. }
  5727. }
  5728. }
  5729. }
  5730. }
  5731. namespace {
  5732. cm::string_view missingTargetPossibleReasons =
  5733. "Possible reasons include:\n"
  5734. " * There is a typo in the target name.\n"
  5735. " * A find_package call is missing for an IMPORTED target.\n"
  5736. " * An ALIAS target is missing.\n"_s;
  5737. }
  5738. bool cmGeneratorTarget::VerifyLinkItemColons(LinkItemRole role,
  5739. cmLinkItem const& item) const
  5740. {
  5741. if (item.Target || cmHasPrefix(item.AsStr(), "<LINK_GROUP:"_s) ||
  5742. item.AsStr().find("::") == std::string::npos) {
  5743. return true;
  5744. }
  5745. MessageType messageType = MessageType::FATAL_ERROR;
  5746. std::string e;
  5747. switch (this->GetLocalGenerator()->GetPolicyStatus(cmPolicies::CMP0028)) {
  5748. case cmPolicies::WARN: {
  5749. e = cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0028), "\n");
  5750. messageType = MessageType::AUTHOR_WARNING;
  5751. } break;
  5752. case cmPolicies::OLD:
  5753. return true;
  5754. case cmPolicies::REQUIRED_IF_USED:
  5755. case cmPolicies::REQUIRED_ALWAYS:
  5756. case cmPolicies::NEW:
  5757. // Issue the fatal message.
  5758. break;
  5759. }
  5760. if (role == LinkItemRole::Implementation) {
  5761. e = cmStrCat(e, "Target \"", this->GetName(), "\" links to");
  5762. } else {
  5763. e = cmStrCat(e, "The link interface of target \"", this->GetName(),
  5764. "\" contains");
  5765. }
  5766. e =
  5767. cmStrCat(e, ":\n ", item.AsStr(), "\n", "but the target was not found. ",
  5768. missingTargetPossibleReasons);
  5769. cmListFileBacktrace backtrace = item.Backtrace;
  5770. if (backtrace.Empty()) {
  5771. backtrace = this->GetBacktrace();
  5772. }
  5773. this->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(messageType, e,
  5774. backtrace);
  5775. return false;
  5776. }
  5777. bool cmGeneratorTarget::VerifyLinkItemIsTarget(LinkItemRole role,
  5778. cmLinkItem const& item) const
  5779. {
  5780. if (item.Target) {
  5781. return true;
  5782. }
  5783. std::string const& str = item.AsStr();
  5784. if (!str.empty() &&
  5785. (str[0] == '-' || str[0] == '$' || str[0] == '`' ||
  5786. str.find_first_of("/\\") != std::string::npos ||
  5787. cmHasPrefix(str, "<LINK_LIBRARY:"_s) ||
  5788. cmHasPrefix(str, "<LINK_GROUP:"_s))) {
  5789. return true;
  5790. }
  5791. std::string e = cmStrCat("Target \"", this->GetName(),
  5792. "\" has LINK_LIBRARIES_ONLY_TARGETS enabled, but ",
  5793. role == LinkItemRole::Implementation
  5794. ? "it links to"
  5795. : "its link interface contains",
  5796. ":\n ", item.AsStr(), "\nwhich is not a target. ",
  5797. missingTargetPossibleReasons);
  5798. cmListFileBacktrace backtrace = item.Backtrace;
  5799. if (backtrace.Empty()) {
  5800. backtrace = this->GetBacktrace();
  5801. }
  5802. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5803. MessageType::FATAL_ERROR, e, backtrace);
  5804. return false;
  5805. }
  5806. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  5807. {
  5808. int patch;
  5809. this->GetTargetVersion("VERSION", major, minor, patch);
  5810. }
  5811. void cmGeneratorTarget::GetTargetVersionFallback(
  5812. const std::string& property, const std::string& fallback_property,
  5813. int& major, int& minor, int& patch) const
  5814. {
  5815. if (this->GetProperty(property)) {
  5816. this->GetTargetVersion(property, major, minor, patch);
  5817. } else {
  5818. this->GetTargetVersion(fallback_property, major, minor, patch);
  5819. }
  5820. }
  5821. void cmGeneratorTarget::GetTargetVersion(const std::string& property,
  5822. int& major, int& minor,
  5823. int& patch) const
  5824. {
  5825. // Set the default values.
  5826. major = 0;
  5827. minor = 0;
  5828. patch = 0;
  5829. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5830. if (cmValue version = this->GetProperty(property)) {
  5831. // Try to parse the version number and store the results that were
  5832. // successfully parsed.
  5833. int parsed_major;
  5834. int parsed_minor;
  5835. int parsed_patch;
  5836. switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
  5837. &parsed_patch)) {
  5838. case 3:
  5839. patch = parsed_patch;
  5840. CM_FALLTHROUGH;
  5841. case 2:
  5842. minor = parsed_minor;
  5843. CM_FALLTHROUGH;
  5844. case 1:
  5845. major = parsed_major;
  5846. CM_FALLTHROUGH;
  5847. default:
  5848. break;
  5849. }
  5850. }
  5851. }
  5852. std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
  5853. std::string const& lang, std::string const& config) const
  5854. {
  5855. // This is activated by the presence of a default selection whether or
  5856. // not it is overridden by a property.
  5857. cmValue runtimeLibraryDefault = this->Makefile->GetDefinition(
  5858. cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
  5859. if (!cmNonempty(runtimeLibraryDefault)) {
  5860. return std::string();
  5861. }
  5862. cmValue runtimeLibraryValue =
  5863. this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
  5864. if (!runtimeLibraryValue) {
  5865. runtimeLibraryValue = runtimeLibraryDefault;
  5866. }
  5867. return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
  5868. *runtimeLibraryValue, this->LocalGenerator, config, this));
  5869. }
  5870. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  5871. std::string const& working_dir) const
  5872. {
  5873. if (!this->FortranModuleDirectoryCreated) {
  5874. this->FortranModuleDirectory =
  5875. this->CreateFortranModuleDirectory(working_dir);
  5876. this->FortranModuleDirectoryCreated = true;
  5877. }
  5878. return this->FortranModuleDirectory;
  5879. }
  5880. bool cmGeneratorTarget::IsFortranBuildingInstrinsicModules() const
  5881. {
  5882. if (cmValue prop =
  5883. this->GetProperty("Fortran_BUILDING_INSTRINSIC_MODULES")) {
  5884. return cmIsOn(*prop);
  5885. }
  5886. return false;
  5887. }
  5888. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  5889. std::string const& working_dir) const
  5890. {
  5891. std::string mod_dir;
  5892. std::string target_mod_dir;
  5893. if (cmValue prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  5894. target_mod_dir = *prop;
  5895. } else {
  5896. std::string const& default_mod_dir =
  5897. this->LocalGenerator->GetCurrentBinaryDirectory();
  5898. if (default_mod_dir != working_dir) {
  5899. target_mod_dir = default_mod_dir;
  5900. }
  5901. }
  5902. cmValue moddir_flag =
  5903. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  5904. if (!target_mod_dir.empty() && moddir_flag) {
  5905. // Compute the full path to the module directory.
  5906. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  5907. // Already a full path.
  5908. mod_dir = target_mod_dir;
  5909. } else {
  5910. // Interpret relative to the current output directory.
  5911. mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  5912. '/', target_mod_dir);
  5913. }
  5914. // Make sure the module output directory exists.
  5915. cmSystemTools::MakeDirectory(mod_dir);
  5916. }
  5917. return mod_dir;
  5918. }
  5919. void cmGeneratorTarget::AddISPCGeneratedHeader(std::string const& header,
  5920. std::string const& config)
  5921. {
  5922. std::string config_upper;
  5923. if (!config.empty()) {
  5924. config_upper = cmSystemTools::UpperCase(config);
  5925. }
  5926. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5927. if (iter == this->ISPCGeneratedHeaders.end()) {
  5928. std::vector<std::string> headers;
  5929. headers.emplace_back(header);
  5930. this->ISPCGeneratedHeaders.insert({ config_upper, headers });
  5931. } else {
  5932. iter->second.emplace_back(header);
  5933. }
  5934. }
  5935. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCHeaders(
  5936. std::string const& config) const
  5937. {
  5938. std::string config_upper;
  5939. if (!config.empty()) {
  5940. config_upper = cmSystemTools::UpperCase(config);
  5941. }
  5942. auto iter = this->ISPCGeneratedHeaders.find(config_upper);
  5943. if (iter == this->ISPCGeneratedHeaders.end()) {
  5944. return std::vector<std::string>{};
  5945. }
  5946. return iter->second;
  5947. }
  5948. void cmGeneratorTarget::AddISPCGeneratedObject(std::vector<std::string>&& objs,
  5949. std::string const& config)
  5950. {
  5951. std::string config_upper;
  5952. if (!config.empty()) {
  5953. config_upper = cmSystemTools::UpperCase(config);
  5954. }
  5955. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  5956. if (iter == this->ISPCGeneratedObjects.end()) {
  5957. this->ISPCGeneratedObjects.insert({ config_upper, objs });
  5958. } else {
  5959. iter->second.insert(iter->second.end(), objs.begin(), objs.end());
  5960. }
  5961. }
  5962. std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCObjects(
  5963. std::string const& config) const
  5964. {
  5965. std::string config_upper;
  5966. if (!config.empty()) {
  5967. config_upper = cmSystemTools::UpperCase(config);
  5968. }
  5969. auto iter = this->ISPCGeneratedObjects.find(config_upper);
  5970. if (iter == this->ISPCGeneratedObjects.end()) {
  5971. return std::vector<std::string>{};
  5972. }
  5973. return iter->second;
  5974. }
  5975. std::string cmGeneratorTarget::GetFrameworkVersion() const
  5976. {
  5977. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5978. if (cmValue fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  5979. return *fversion;
  5980. }
  5981. if (cmValue tversion = this->GetProperty("VERSION")) {
  5982. return *tversion;
  5983. }
  5984. return "A";
  5985. }
  5986. void cmGeneratorTarget::ComputeVersionedName(
  5987. std::string& vName, std::string const& prefix, std::string const& base,
  5988. std::string const& suffix, std::string const& name, cmValue version) const
  5989. {
  5990. vName = this->IsApple() ? (prefix + base) : name;
  5991. if (version) {
  5992. vName += ".";
  5993. vName += *version;
  5994. }
  5995. vName += this->IsApple() ? suffix : std::string();
  5996. }
  5997. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  5998. {
  5999. return this->Target->GetProperties().GetKeys();
  6000. }
  6001. void cmGeneratorTarget::ReportPropertyOrigin(
  6002. const std::string& p, const std::string& result, const std::string& report,
  6003. const std::string& compatibilityType) const
  6004. {
  6005. cmList debugProperties{ this->Target->GetMakefile()->GetDefinition(
  6006. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  6007. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  6008. cm::contains(debugProperties, p);
  6009. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  6010. this->DebugCompatiblePropertiesDone[p] = true;
  6011. }
  6012. if (!debugOrigin) {
  6013. return;
  6014. }
  6015. std::string areport =
  6016. cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
  6017. this->GetName(), "\" (result: \"", result, "\"):\n", report);
  6018. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  6019. areport);
  6020. }
  6021. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  6022. cmLocalGenerator const*& lg) const
  6023. {
  6024. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  6025. cmDirectoryId const dirId = n.substr(cmStrLen(CMAKE_DIRECTORY_ID_SEP));
  6026. if (dirId.String.empty()) {
  6027. lg = this->LocalGenerator;
  6028. return true;
  6029. }
  6030. if (cmLocalGenerator const* otherLG =
  6031. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  6032. lg = otherLG;
  6033. return true;
  6034. }
  6035. }
  6036. return false;
  6037. }
  6038. cm::optional<cmLinkItem> cmGeneratorTarget::LookupLinkItem(
  6039. std::string const& n, cmListFileBacktrace const& bt,
  6040. LookupLinkItemScope* scope, LookupSelf lookupSelf) const
  6041. {
  6042. cm::optional<cmLinkItem> maybeItem;
  6043. if (this->IsLinkLookupScope(n, scope->LG)) {
  6044. return maybeItem;
  6045. }
  6046. std::string name = this->CheckCMP0004(n);
  6047. if (name.empty() ||
  6048. (lookupSelf == LookupSelf::No && name == this->GetName())) {
  6049. return maybeItem;
  6050. }
  6051. maybeItem = this->ResolveLinkItem(BT<std::string>(name, bt), scope->LG);
  6052. return maybeItem;
  6053. }
  6054. void cmGeneratorTarget::ExpandLinkItems(
  6055. std::string const& prop, cmBTStringRange entries, std::string const& config,
  6056. cmGeneratorTarget const* headTarget, LinkInterfaceFor interfaceFor,
  6057. LinkInterfaceField field, cmLinkInterface& iface) const
  6058. {
  6059. if (entries.empty()) {
  6060. return;
  6061. }
  6062. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  6063. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  6064. // The $<LINK_ONLY> expression may be in a link interface to specify
  6065. // private link dependencies that are otherwise excluded from usage
  6066. // requirements.
  6067. if (interfaceFor == LinkInterfaceFor::Usage) {
  6068. dagChecker.SetTransitivePropertiesOnly();
  6069. dagChecker.SetTransitivePropertiesOnlyCMP0131();
  6070. }
  6071. cmMakefile const* mf = this->LocalGenerator->GetMakefile();
  6072. LookupLinkItemScope scope{ this->LocalGenerator };
  6073. for (BT<std::string> const& entry : entries) {
  6074. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  6075. entry.Backtrace);
  6076. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(entry.Value);
  6077. cge->SetEvaluateForBuildsystem(true);
  6078. cmList libs{ cge->Evaluate(this->LocalGenerator, config, headTarget,
  6079. &dagChecker, this,
  6080. headTarget->LinkerLanguage) };
  6081. for (auto const& lib : libs) {
  6082. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  6083. lib, cge->GetBacktrace(), &scope,
  6084. field == LinkInterfaceField::Libraries ? LookupSelf::No
  6085. : LookupSelf::Yes)) {
  6086. cmLinkItem item = std::move(*maybeItem);
  6087. if (field == LinkInterfaceField::HeadInclude) {
  6088. iface.HeadInclude.emplace_back(std::move(item));
  6089. continue;
  6090. }
  6091. if (field == LinkInterfaceField::HeadExclude) {
  6092. iface.HeadExclude.emplace_back(std::move(item));
  6093. continue;
  6094. }
  6095. if (!item.Target) {
  6096. // Report explicitly linked object files separately.
  6097. std::string const& maybeObj = item.AsStr();
  6098. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  6099. cmSourceFile const* sf =
  6100. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  6101. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  6102. iface.Objects.emplace_back(std::move(item));
  6103. continue;
  6104. }
  6105. }
  6106. }
  6107. iface.Libraries.emplace_back(std::move(item));
  6108. }
  6109. }
  6110. if (cge->GetHadHeadSensitiveCondition()) {
  6111. iface.HadHeadSensitiveCondition = true;
  6112. }
  6113. if (cge->GetHadContextSensitiveCondition()) {
  6114. iface.HadContextSensitiveCondition = true;
  6115. }
  6116. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  6117. iface.HadLinkLanguageSensitiveCondition = true;
  6118. }
  6119. }
  6120. }
  6121. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  6122. const std::string& config, cmGeneratorTarget const* head) const
  6123. {
  6124. return this->GetLinkInterface(config, head, false);
  6125. }
  6126. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  6127. const std::string& config, cmGeneratorTarget const* head,
  6128. bool secondPass) const
  6129. {
  6130. // Imported targets have their own link interface.
  6131. if (this->IsImported()) {
  6132. return this->GetImportLinkInterface(config, head, LinkInterfaceFor::Link,
  6133. secondPass);
  6134. }
  6135. // Link interfaces are not supported for executables that do not
  6136. // export symbols.
  6137. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  6138. !this->IsExecutableWithExports()) {
  6139. return nullptr;
  6140. }
  6141. // Lookup any existing link interface for this configuration.
  6142. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  6143. // If the link interface does not depend on the head target
  6144. // then re-use the one from the head we computed first.
  6145. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6146. head = hm.begin()->first;
  6147. }
  6148. cmOptionalLinkInterface& iface = hm[head];
  6149. if (secondPass) {
  6150. iface = cmOptionalLinkInterface();
  6151. }
  6152. if (!iface.LibrariesDone) {
  6153. iface.LibrariesDone = true;
  6154. this->ComputeLinkInterfaceLibraries(config, iface, head,
  6155. LinkInterfaceFor::Link);
  6156. }
  6157. if (!iface.AllDone) {
  6158. iface.AllDone = true;
  6159. if (iface.Exists) {
  6160. this->ComputeLinkInterface(config, iface, head, secondPass);
  6161. this->ComputeLinkInterfaceRuntimeLibraries(config, iface);
  6162. }
  6163. }
  6164. return iface.Exists ? &iface : nullptr;
  6165. }
  6166. void cmGeneratorTarget::ComputeLinkInterface(
  6167. const std::string& config, cmOptionalLinkInterface& iface,
  6168. cmGeneratorTarget const* headTarget) const
  6169. {
  6170. this->ComputeLinkInterface(config, iface, headTarget, false);
  6171. }
  6172. void cmGeneratorTarget::ComputeLinkInterface(
  6173. const std::string& config, cmOptionalLinkInterface& iface,
  6174. cmGeneratorTarget const* headTarget, bool secondPass) const
  6175. {
  6176. if (iface.Explicit) {
  6177. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6178. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6179. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6180. // Shared libraries may have runtime implementation dependencies
  6181. // on other shared libraries that are not in the interface.
  6182. std::set<cmLinkItem> emitted;
  6183. for (cmLinkItem const& lib : iface.Libraries) {
  6184. emitted.insert(lib);
  6185. }
  6186. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  6187. cmLinkImplementation const* impl = this->GetLinkImplementation(
  6188. config, LinkInterfaceFor::Link, secondPass);
  6189. for (cmLinkImplItem const& lib : impl->Libraries) {
  6190. if (emitted.insert(lib).second) {
  6191. if (lib.Target) {
  6192. // This is a runtime dependency on another shared library.
  6193. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6194. iface.SharedDeps.push_back(lib);
  6195. }
  6196. } else {
  6197. // TODO: Recognize shared library file names. Perhaps this
  6198. // should be moved to cmComputeLinkInformation, but that
  6199. // creates a chicken-and-egg problem since this list is needed
  6200. // for its construction.
  6201. }
  6202. }
  6203. }
  6204. }
  6205. }
  6206. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  6207. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  6208. // The link implementation is the default link interface.
  6209. cmLinkImplementationLibraries const* impl =
  6210. this->GetLinkImplementationLibrariesInternal(config, headTarget,
  6211. LinkInterfaceFor::Link);
  6212. iface.ImplementationIsInterface = true;
  6213. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  6214. }
  6215. if (this->LinkLanguagePropagatesToDependents()) {
  6216. // Targets using this archive need its language runtime libraries.
  6217. if (cmLinkImplementation const* impl = this->GetLinkImplementation(
  6218. config, LinkInterfaceFor::Link, secondPass)) {
  6219. iface.Languages = impl->Languages;
  6220. }
  6221. }
  6222. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6223. // Construct the property name suffix for this configuration.
  6224. std::string suffix = "_";
  6225. if (!config.empty()) {
  6226. suffix += cmSystemTools::UpperCase(config);
  6227. } else {
  6228. suffix += "NOCONFIG";
  6229. }
  6230. // How many repetitions are needed if this library has cyclic
  6231. // dependencies?
  6232. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  6233. if (cmValue config_reps = this->GetProperty(propName)) {
  6234. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  6235. } else if (cmValue reps =
  6236. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  6237. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  6238. }
  6239. }
  6240. }
  6241. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  6242. const std::string& config, cmGeneratorTarget const* head,
  6243. LinkInterfaceFor interfaceFor) const
  6244. {
  6245. // Imported targets have their own link interface.
  6246. if (this->IsImported()) {
  6247. return this->GetImportLinkInterface(config, head, interfaceFor);
  6248. }
  6249. // Link interfaces are not supported for executables that do not
  6250. // export symbols.
  6251. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  6252. !this->IsExecutableWithExports()) {
  6253. return nullptr;
  6254. }
  6255. // Lookup any existing link interface for this configuration.
  6256. cmHeadToLinkInterfaceMap& hm =
  6257. (interfaceFor == LinkInterfaceFor::Usage
  6258. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  6259. : this->GetHeadToLinkInterfaceMap(config));
  6260. // If the link interface does not depend on the head target
  6261. // then re-use the one from the head we computed first.
  6262. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6263. head = hm.begin()->first;
  6264. }
  6265. cmOptionalLinkInterface& iface = hm[head];
  6266. if (!iface.LibrariesDone) {
  6267. iface.LibrariesDone = true;
  6268. this->ComputeLinkInterfaceLibraries(config, iface, head, interfaceFor);
  6269. }
  6270. return iface.Exists ? &iface : nullptr;
  6271. }
  6272. std::string cmGeneratorTarget::GetDirectory(
  6273. const std::string& config, cmStateEnums::ArtifactType artifact) const
  6274. {
  6275. if (this->IsImported()) {
  6276. auto fullPath = this->Target->ImportedGetFullPath(config, artifact);
  6277. if (this->IsFrameworkOnApple()) {
  6278. auto fwDescriptor = this->GetGlobalGenerator()->SplitFrameworkPath(
  6279. fullPath, cmGlobalGenerator::FrameworkFormat::Strict);
  6280. if (fwDescriptor) {
  6281. return fwDescriptor->Directory;
  6282. }
  6283. }
  6284. // Return the directory from which the target is imported.
  6285. return cmSystemTools::GetFilenamePath(fullPath);
  6286. }
  6287. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  6288. // Return the directory in which the target will be built.
  6289. switch (artifact) {
  6290. case cmStateEnums::RuntimeBinaryArtifact:
  6291. return info->OutDir;
  6292. case cmStateEnums::ImportLibraryArtifact:
  6293. return info->ImpDir;
  6294. }
  6295. }
  6296. return "";
  6297. }
  6298. bool cmGeneratorTarget::UsesDefaultOutputDir(
  6299. const std::string& config, cmStateEnums::ArtifactType artifact) const
  6300. {
  6301. std::string dir;
  6302. return this->ComputeOutputDir(config, artifact, dir);
  6303. }
  6304. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  6305. const std::string& config) const
  6306. {
  6307. // There is no output information for imported targets.
  6308. if (this->IsImported()) {
  6309. return nullptr;
  6310. }
  6311. // Synthetic targets don't have output.
  6312. if (this->IsSynthetic()) {
  6313. return nullptr;
  6314. }
  6315. // Only libraries and executables have well-defined output files.
  6316. if (!this->HaveWellDefinedOutputFiles()) {
  6317. std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
  6318. this->GetName(), " which has type ",
  6319. cmState::GetTargetTypeName(this->GetType()));
  6320. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  6321. return nullptr;
  6322. }
  6323. // Lookup/compute/cache the output information for this configuration.
  6324. std::string config_upper;
  6325. if (!config.empty()) {
  6326. config_upper = cmSystemTools::UpperCase(config);
  6327. }
  6328. auto i = this->OutputInfoMap.find(config_upper);
  6329. if (i == this->OutputInfoMap.end()) {
  6330. // Add empty info in map to detect potential recursion.
  6331. OutputInfo info;
  6332. OutputInfoMapType::value_type entry(config_upper, info);
  6333. i = this->OutputInfoMap.insert(entry).first;
  6334. // Compute output directories.
  6335. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  6336. info.OutDir);
  6337. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  6338. info.ImpDir);
  6339. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  6340. info.PdbDir = info.OutDir;
  6341. }
  6342. // Now update the previously-prepared map entry.
  6343. i->second = info;
  6344. } else if (i->second.empty()) {
  6345. // An empty map entry indicates we have been called recursively
  6346. // from the above block.
  6347. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6348. MessageType::FATAL_ERROR,
  6349. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  6350. this->GetBacktrace());
  6351. return nullptr;
  6352. }
  6353. return &i->second;
  6354. }
  6355. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  6356. cmStateEnums::ArtifactType artifact,
  6357. std::string& out) const
  6358. {
  6359. bool usesDefaultOutputDir = false;
  6360. std::string conf = config;
  6361. // Look for a target property defining the target output directory
  6362. // based on the target type.
  6363. std::string targetTypeName = this->GetOutputTargetType(artifact);
  6364. std::string propertyName;
  6365. if (!targetTypeName.empty()) {
  6366. propertyName = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY");
  6367. }
  6368. // Check for a per-configuration output directory target property.
  6369. std::string configUpper = cmSystemTools::UpperCase(conf);
  6370. std::string configProp;
  6371. if (!targetTypeName.empty()) {
  6372. configProp = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY_", configUpper);
  6373. }
  6374. // Select an output directory.
  6375. if (cmValue config_outdir = this->GetProperty(configProp)) {
  6376. // Use the user-specified per-configuration output directory.
  6377. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  6378. config, this);
  6379. // Skip per-configuration subdirectory.
  6380. conf.clear();
  6381. } else if (cmValue outdir = this->GetProperty(propertyName)) {
  6382. // Use the user-specified output directory.
  6383. out = cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator,
  6384. config, this);
  6385. // Skip per-configuration subdirectory if the value contained a
  6386. // generator expression.
  6387. if (out != *outdir) {
  6388. conf.clear();
  6389. }
  6390. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  6391. // Lookup the output path for executables.
  6392. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  6393. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6394. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6395. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  6396. // Lookup the output path for libraries.
  6397. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  6398. }
  6399. if (out.empty()) {
  6400. // Default to the current output directory.
  6401. usesDefaultOutputDir = true;
  6402. out = ".";
  6403. }
  6404. // Convert the output path to a full path in case it is
  6405. // specified as a relative path. Treat a relative path as
  6406. // relative to the current output directory for this makefile.
  6407. out = (cmSystemTools::CollapseFullPath(
  6408. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  6409. // The generator may add the configuration's subdirectory.
  6410. if (!conf.empty()) {
  6411. bool useEPN =
  6412. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  6413. std::string suffix =
  6414. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  6415. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  6416. "/", conf, suffix, out);
  6417. }
  6418. return usesDefaultOutputDir;
  6419. }
  6420. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  6421. const std::string& config,
  6422. std::string& out) const
  6423. {
  6424. // Look for a target property defining the target output directory
  6425. // based on the target type.
  6426. std::string propertyName;
  6427. if (!kind.empty()) {
  6428. propertyName = cmStrCat(kind, "_OUTPUT_DIRECTORY");
  6429. }
  6430. std::string conf = config;
  6431. // Check for a per-configuration output directory target property.
  6432. std::string configUpper = cmSystemTools::UpperCase(conf);
  6433. std::string configProp;
  6434. if (!kind.empty()) {
  6435. configProp = cmStrCat(kind, "_OUTPUT_DIRECTORY_", configUpper);
  6436. }
  6437. // Select an output directory.
  6438. if (cmValue config_outdir = this->GetProperty(configProp)) {
  6439. // Use the user-specified per-configuration output directory.
  6440. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  6441. config);
  6442. // Skip per-configuration subdirectory.
  6443. conf.clear();
  6444. } else if (cmValue outdir = this->GetProperty(propertyName)) {
  6445. // Use the user-specified output directory.
  6446. out =
  6447. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  6448. // Skip per-configuration subdirectory if the value contained a
  6449. // generator expression.
  6450. if (out != *outdir) {
  6451. conf.clear();
  6452. }
  6453. }
  6454. if (out.empty()) {
  6455. return false;
  6456. }
  6457. // Convert the output path to a full path in case it is
  6458. // specified as a relative path. Treat a relative path as
  6459. // relative to the current output directory for this makefile.
  6460. out = (cmSystemTools::CollapseFullPath(
  6461. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  6462. // The generator may add the configuration's subdirectory.
  6463. if (!conf.empty()) {
  6464. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  6465. "/", conf, "", out);
  6466. }
  6467. return true;
  6468. }
  6469. bool cmGeneratorTarget::HaveInstallTreeRPATH(const std::string& config) const
  6470. {
  6471. std::string install_rpath;
  6472. this->GetInstallRPATH(config, install_rpath);
  6473. return !install_rpath.empty() &&
  6474. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  6475. }
  6476. bool cmGeneratorTarget::GetBuildRPATH(const std::string& config,
  6477. std::string& rpath) const
  6478. {
  6479. return this->GetRPATH(config, "BUILD_RPATH", rpath);
  6480. }
  6481. bool cmGeneratorTarget::GetInstallRPATH(const std::string& config,
  6482. std::string& rpath) const
  6483. {
  6484. return this->GetRPATH(config, "INSTALL_RPATH", rpath);
  6485. }
  6486. bool cmGeneratorTarget::GetRPATH(const std::string& config,
  6487. const std::string& prop,
  6488. std::string& rpath) const
  6489. {
  6490. cmValue value = this->GetProperty(prop);
  6491. if (!value) {
  6492. return false;
  6493. }
  6494. rpath =
  6495. cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
  6496. return true;
  6497. }
  6498. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  6499. const std::string& config, cmOptionalLinkInterface& iface,
  6500. cmGeneratorTarget const* headTarget, LinkInterfaceFor interfaceFor) const
  6501. {
  6502. // Construct the property name suffix for this configuration.
  6503. std::string suffix = "_";
  6504. if (!config.empty()) {
  6505. suffix += cmSystemTools::UpperCase(config);
  6506. } else {
  6507. suffix += "NOCONFIG";
  6508. }
  6509. // An explicit list of interface libraries may be set for shared
  6510. // libraries and executables that export symbols.
  6511. bool haveExplicitLibraries = false;
  6512. cmValue explicitLibrariesCMP0022OLD;
  6513. std::string linkIfacePropCMP0022OLD;
  6514. bool const cmp0022NEW = (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  6515. this->GetPolicyStatusCMP0022() != cmPolicies::WARN);
  6516. if (cmp0022NEW) {
  6517. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  6518. haveExplicitLibraries = !this->Target->GetLinkInterfaceEntries().empty() ||
  6519. !this->Target->GetLinkInterfaceDirectEntries().empty() ||
  6520. !this->Target->GetLinkInterfaceDirectExcludeEntries().empty();
  6521. } else {
  6522. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  6523. // shared lib or executable.
  6524. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6525. this->IsExecutableWithExports()) {
  6526. // Lookup the per-configuration property.
  6527. linkIfacePropCMP0022OLD = cmStrCat("LINK_INTERFACE_LIBRARIES", suffix);
  6528. explicitLibrariesCMP0022OLD = this->GetProperty(linkIfacePropCMP0022OLD);
  6529. // If not set, try the generic property.
  6530. if (!explicitLibrariesCMP0022OLD) {
  6531. linkIfacePropCMP0022OLD = "LINK_INTERFACE_LIBRARIES";
  6532. explicitLibrariesCMP0022OLD =
  6533. this->GetProperty(linkIfacePropCMP0022OLD);
  6534. }
  6535. }
  6536. if (explicitLibrariesCMP0022OLD &&
  6537. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  6538. !this->PolicyWarnedCMP0022) {
  6539. // Compare the explicitly set old link interface properties to the
  6540. // preferred new link interface property one and warn if different.
  6541. cmValue newExplicitLibraries =
  6542. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  6543. if (newExplicitLibraries &&
  6544. (*newExplicitLibraries != *explicitLibrariesCMP0022OLD)) {
  6545. std::ostringstream w;
  6546. /* clang-format off */
  6547. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  6548. "Target \"" << this->GetName() << "\" has an "
  6549. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  6550. linkIfacePropCMP0022OLD << " properties."
  6551. "\n"
  6552. "INTERFACE_LINK_LIBRARIES:\n"
  6553. " " << *newExplicitLibraries << "\n" <<
  6554. linkIfacePropCMP0022OLD << ":\n"
  6555. " " << *explicitLibrariesCMP0022OLD << "\n";
  6556. /* clang-format on */
  6557. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  6558. w.str());
  6559. this->PolicyWarnedCMP0022 = true;
  6560. }
  6561. }
  6562. haveExplicitLibraries = static_cast<bool>(explicitLibrariesCMP0022OLD);
  6563. }
  6564. // There is no implicit link interface for executables or modules
  6565. // so if none was explicitly set then there is no link interface.
  6566. if (!haveExplicitLibraries &&
  6567. (this->GetType() == cmStateEnums::EXECUTABLE ||
  6568. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  6569. return;
  6570. }
  6571. iface.Exists = true;
  6572. // If CMP0022 is NEW then the plain tll signature sets the
  6573. // INTERFACE_LINK_LIBRARIES property. Even if the project
  6574. // clears it, the link interface is still explicit.
  6575. iface.Explicit = cmp0022NEW || explicitLibrariesCMP0022OLD;
  6576. if (cmp0022NEW) {
  6577. // The interface libraries are specified by INTERFACE_LINK_LIBRARIES.
  6578. // Use its special representation directly to get backtraces.
  6579. this->ExpandLinkItems(
  6580. kINTERFACE_LINK_LIBRARIES, this->Target->GetLinkInterfaceEntries(),
  6581. config, headTarget, interfaceFor, LinkInterfaceField::Libraries, iface);
  6582. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  6583. this->Target->GetLinkInterfaceDirectEntries(),
  6584. config, headTarget, interfaceFor,
  6585. LinkInterfaceField::HeadInclude, iface);
  6586. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  6587. this->Target->GetLinkInterfaceDirectExcludeEntries(),
  6588. config, headTarget, interfaceFor,
  6589. LinkInterfaceField::HeadExclude, iface);
  6590. } else if (explicitLibrariesCMP0022OLD) {
  6591. // The interface libraries have been explicitly set in pre-CMP0022 style.
  6592. std::vector<BT<std::string>> entries;
  6593. entries.emplace_back(*explicitLibrariesCMP0022OLD);
  6594. this->ExpandLinkItems(linkIfacePropCMP0022OLD, cmMakeRange(entries),
  6595. config, headTarget, interfaceFor,
  6596. LinkInterfaceField::Libraries, iface);
  6597. }
  6598. // If the link interface is explicit, do not fall back to the link impl.
  6599. if (iface.Explicit) {
  6600. return;
  6601. }
  6602. // The link implementation is the default link interface.
  6603. if (cmLinkImplementationLibraries const* impl =
  6604. this->GetLinkImplementationLibrariesInternal(config, headTarget,
  6605. interfaceFor)) {
  6606. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  6607. impl->Libraries.end());
  6608. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  6609. !this->PolicyWarnedCMP0022 && interfaceFor == LinkInterfaceFor::Link) {
  6610. // Compare the link implementation fallback link interface to the
  6611. // preferred new link interface property and warn if different.
  6612. cmLinkInterface ifaceNew;
  6613. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES,
  6614. this->Target->GetLinkInterfaceEntries(), config,
  6615. headTarget, interfaceFor,
  6616. LinkInterfaceField::Libraries, ifaceNew);
  6617. if (ifaceNew.Libraries != iface.Libraries) {
  6618. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  6619. std::string newLibraries = cmJoin(ifaceNew.Libraries, ";");
  6620. if (oldLibraries.empty()) {
  6621. oldLibraries = "(empty)";
  6622. }
  6623. if (newLibraries.empty()) {
  6624. newLibraries = "(empty)";
  6625. }
  6626. std::ostringstream w;
  6627. /* clang-format off */
  6628. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  6629. "Target \"" << this->GetName() << "\" has an "
  6630. "INTERFACE_LINK_LIBRARIES property. "
  6631. "This should be preferred as the source of the link interface "
  6632. "for this library but because CMP0022 is not set CMake is "
  6633. "ignoring the property and using the link implementation "
  6634. "as the link interface instead."
  6635. "\n"
  6636. "INTERFACE_LINK_LIBRARIES:\n"
  6637. " " << newLibraries << "\n"
  6638. "Link implementation:\n"
  6639. " " << oldLibraries << "\n";
  6640. /* clang-format on */
  6641. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  6642. w.str());
  6643. this->PolicyWarnedCMP0022 = true;
  6644. }
  6645. }
  6646. }
  6647. }
  6648. namespace {
  6649. template <typename ReturnType>
  6650. ReturnType constructItem(cmGeneratorTarget* target,
  6651. cmListFileBacktrace const& bt);
  6652. template <>
  6653. inline cmLinkImplItem constructItem(cmGeneratorTarget* target,
  6654. cmListFileBacktrace const& bt)
  6655. {
  6656. return cmLinkImplItem(cmLinkItem(target, false, bt), false);
  6657. }
  6658. template <>
  6659. inline cmLinkItem constructItem(cmGeneratorTarget* target,
  6660. cmListFileBacktrace const& bt)
  6661. {
  6662. return cmLinkItem(target, false, bt);
  6663. }
  6664. template <typename ValueType>
  6665. std::vector<ValueType> computeImplicitLanguageTargets(
  6666. std::string const& lang, std::string const& config,
  6667. cmGeneratorTarget const* currentTarget)
  6668. {
  6669. cmListFileBacktrace bt;
  6670. std::vector<ValueType> result;
  6671. cmLocalGenerator* lg = currentTarget->GetLocalGenerator();
  6672. std::string const& runtimeLibrary =
  6673. currentTarget->GetRuntimeLinkLibrary(lang, config);
  6674. if (cmValue runtimeLinkOptions = currentTarget->Makefile->GetDefinition(
  6675. "CMAKE_" + lang + "_RUNTIME_LIBRARIES_" + runtimeLibrary)) {
  6676. cmList libsList{ *runtimeLinkOptions };
  6677. result.reserve(libsList.size());
  6678. for (auto const& i : libsList) {
  6679. cmGeneratorTarget::TargetOrString resolved =
  6680. currentTarget->ResolveTargetReference(i, lg);
  6681. if (resolved.Target) {
  6682. result.emplace_back(constructItem<ValueType>(resolved.Target, bt));
  6683. }
  6684. }
  6685. }
  6686. return result;
  6687. }
  6688. }
  6689. void cmGeneratorTarget::ComputeLinkInterfaceRuntimeLibraries(
  6690. const std::string& config, cmOptionalLinkInterface& iface) const
  6691. {
  6692. for (std::string const& lang : iface.Languages) {
  6693. if ((lang == "CUDA" || lang == "HIP") &&
  6694. iface.LanguageRuntimeLibraries.find(lang) ==
  6695. iface.LanguageRuntimeLibraries.end()) {
  6696. auto implicitTargets =
  6697. computeImplicitLanguageTargets<cmLinkItem>(lang, config, this);
  6698. iface.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  6699. }
  6700. }
  6701. }
  6702. void cmGeneratorTarget::ComputeLinkImplementationRuntimeLibraries(
  6703. const std::string& config, cmOptionalLinkImplementation& impl) const
  6704. {
  6705. for (std::string const& lang : impl.Languages) {
  6706. if ((lang == "CUDA" || lang == "HIP") &&
  6707. impl.LanguageRuntimeLibraries.find(lang) ==
  6708. impl.LanguageRuntimeLibraries.end()) {
  6709. auto implicitTargets =
  6710. computeImplicitLanguageTargets<cmLinkImplItem>(lang, config, this);
  6711. impl.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  6712. }
  6713. }
  6714. }
  6715. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  6716. const std::string& config, cmGeneratorTarget const* headTarget,
  6717. LinkInterfaceFor interfaceFor, bool secondPass) const
  6718. {
  6719. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  6720. if (!info) {
  6721. return nullptr;
  6722. }
  6723. cmHeadToLinkInterfaceMap& hm =
  6724. (interfaceFor == LinkInterfaceFor::Usage
  6725. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  6726. : this->GetHeadToLinkInterfaceMap(config));
  6727. // If the link interface does not depend on the head target
  6728. // then re-use the one from the head we computed first.
  6729. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6730. headTarget = hm.begin()->first;
  6731. }
  6732. cmOptionalLinkInterface& iface = hm[headTarget];
  6733. if (secondPass) {
  6734. iface = cmOptionalLinkInterface();
  6735. }
  6736. if (!iface.AllDone) {
  6737. iface.AllDone = true;
  6738. iface.LibrariesDone = true;
  6739. iface.Multiplicity = info->Multiplicity;
  6740. cmExpandList(info->Languages, iface.Languages);
  6741. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  6742. cmMakeRange(info->LibrariesHeadInclude), config,
  6743. headTarget, interfaceFor,
  6744. LinkInterfaceField::HeadInclude, iface);
  6745. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  6746. cmMakeRange(info->LibrariesHeadExclude), config,
  6747. headTarget, interfaceFor,
  6748. LinkInterfaceField::HeadExclude, iface);
  6749. this->ExpandLinkItems(info->LibrariesProp, cmMakeRange(info->Libraries),
  6750. config, headTarget, interfaceFor,
  6751. LinkInterfaceField::Libraries, iface);
  6752. cmList deps{ info->SharedDeps };
  6753. LookupLinkItemScope scope{ this->LocalGenerator };
  6754. for (auto const& dep : deps) {
  6755. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  6756. dep, cmListFileBacktrace(), &scope, LookupSelf::No)) {
  6757. iface.SharedDeps.emplace_back(std::move(*maybeItem));
  6758. }
  6759. }
  6760. }
  6761. return &iface;
  6762. }
  6763. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  6764. const std::string& config) const
  6765. {
  6766. // There is no imported information for non-imported targets.
  6767. if (!this->IsImported()) {
  6768. return nullptr;
  6769. }
  6770. // Lookup/compute/cache the import information for this
  6771. // configuration.
  6772. std::string config_upper;
  6773. if (!config.empty()) {
  6774. config_upper = cmSystemTools::UpperCase(config);
  6775. } else {
  6776. config_upper = "NOCONFIG";
  6777. }
  6778. auto i = this->ImportInfoMap.find(config_upper);
  6779. if (i == this->ImportInfoMap.end()) {
  6780. ImportInfo info;
  6781. this->ComputeImportInfo(config_upper, info);
  6782. ImportInfoMapType::value_type entry(config_upper, info);
  6783. i = this->ImportInfoMap.insert(entry).first;
  6784. }
  6785. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6786. return &i->second;
  6787. }
  6788. // If the location is empty then the target is not available for
  6789. // this configuration.
  6790. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  6791. return nullptr;
  6792. }
  6793. // Return the import information.
  6794. return &i->second;
  6795. }
  6796. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  6797. ImportInfo& info) const
  6798. {
  6799. // This method finds information about an imported target from its
  6800. // properties. The "IMPORTED_" namespace is reserved for properties
  6801. // defined by the project exporting the target.
  6802. // Initialize members.
  6803. info.NoSOName = false;
  6804. cmValue loc = nullptr;
  6805. cmValue imp = nullptr;
  6806. std::string suffix;
  6807. if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
  6808. return;
  6809. }
  6810. // Get the link interface.
  6811. {
  6812. // Use the INTERFACE_LINK_LIBRARIES special representation directly
  6813. // to get backtraces.
  6814. cmBTStringRange entries = this->Target->GetLinkInterfaceEntries();
  6815. if (!entries.empty()) {
  6816. info.LibrariesProp = "INTERFACE_LINK_LIBRARIES";
  6817. for (BT<std::string> const& entry : entries) {
  6818. info.Libraries.emplace_back(entry);
  6819. }
  6820. } else if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  6821. std::string linkProp =
  6822. cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
  6823. cmValue propertyLibs = this->GetProperty(linkProp);
  6824. if (!propertyLibs) {
  6825. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  6826. propertyLibs = this->GetProperty(linkProp);
  6827. }
  6828. if (propertyLibs) {
  6829. info.LibrariesProp = linkProp;
  6830. info.Libraries.emplace_back(*propertyLibs);
  6831. }
  6832. }
  6833. }
  6834. for (BT<std::string> const& entry :
  6835. this->Target->GetLinkInterfaceDirectEntries()) {
  6836. info.LibrariesHeadInclude.emplace_back(entry);
  6837. }
  6838. for (BT<std::string> const& entry :
  6839. this->Target->GetLinkInterfaceDirectExcludeEntries()) {
  6840. info.LibrariesHeadExclude.emplace_back(entry);
  6841. }
  6842. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  6843. if (loc) {
  6844. info.LibName = *loc;
  6845. }
  6846. return;
  6847. }
  6848. // A provided configuration has been chosen. Load the
  6849. // configuration's properties.
  6850. // Get the location.
  6851. if (loc) {
  6852. info.Location = *loc;
  6853. } else {
  6854. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  6855. if (cmValue config_location = this->GetProperty(impProp)) {
  6856. info.Location = *config_location;
  6857. } else if (cmValue location = this->GetProperty("IMPORTED_LOCATION")) {
  6858. info.Location = *location;
  6859. }
  6860. }
  6861. // Get the soname.
  6862. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6863. std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
  6864. if (cmValue config_soname = this->GetProperty(soProp)) {
  6865. info.SOName = *config_soname;
  6866. } else if (cmValue soname = this->GetProperty("IMPORTED_SONAME")) {
  6867. info.SOName = *soname;
  6868. }
  6869. }
  6870. // Get the "no-soname" mark.
  6871. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  6872. std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
  6873. if (cmValue config_no_soname = this->GetProperty(soProp)) {
  6874. info.NoSOName = cmIsOn(*config_no_soname);
  6875. } else if (cmValue no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
  6876. info.NoSOName = cmIsOn(*no_soname);
  6877. }
  6878. }
  6879. // Get the import library.
  6880. if (imp) {
  6881. info.ImportLibrary = *imp;
  6882. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6883. this->IsExecutableWithExports()) {
  6884. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  6885. if (cmValue config_implib = this->GetProperty(impProp)) {
  6886. info.ImportLibrary = *config_implib;
  6887. } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
  6888. info.ImportLibrary = *implib;
  6889. }
  6890. }
  6891. // Get the link dependencies.
  6892. {
  6893. std::string linkProp =
  6894. cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
  6895. if (cmValue config_libs = this->GetProperty(linkProp)) {
  6896. info.SharedDeps = *config_libs;
  6897. } else if (cmValue libs =
  6898. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  6899. info.SharedDeps = *libs;
  6900. }
  6901. }
  6902. // Get the link languages.
  6903. if (this->LinkLanguagePropagatesToDependents()) {
  6904. std::string linkProp =
  6905. cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
  6906. if (cmValue config_libs = this->GetProperty(linkProp)) {
  6907. info.Languages = *config_libs;
  6908. } else if (cmValue libs =
  6909. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  6910. info.Languages = *libs;
  6911. }
  6912. }
  6913. // Get information if target is managed assembly.
  6914. {
  6915. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  6916. if (cmValue pc = this->GetProperty(linkProp + suffix)) {
  6917. info.Managed = this->CheckManagedType(*pc);
  6918. } else if (cmValue p = this->GetProperty(linkProp)) {
  6919. info.Managed = this->CheckManagedType(*p);
  6920. }
  6921. }
  6922. // Get the cyclic repetition count.
  6923. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6924. std::string linkProp =
  6925. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  6926. if (cmValue config_reps = this->GetProperty(linkProp)) {
  6927. sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
  6928. } else if (cmValue reps =
  6929. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  6930. sscanf(reps->c_str(), "%u", &info.Multiplicity);
  6931. }
  6932. }
  6933. }
  6934. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  6935. const std::string& config) const
  6936. {
  6937. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  6938. }
  6939. cmHeadToLinkInterfaceMap&
  6940. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  6941. const std::string& config) const
  6942. {
  6943. return this
  6944. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6945. }
  6946. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6947. const std::string& config, LinkInterfaceFor implFor) const
  6948. {
  6949. return this->GetLinkImplementation(config, implFor, false);
  6950. }
  6951. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6952. const std::string& config, LinkInterfaceFor implFor, bool secondPass) const
  6953. {
  6954. // There is no link implementation for targets that cannot compile sources.
  6955. if (!this->CanCompileSources()) {
  6956. return nullptr;
  6957. }
  6958. HeadToLinkImplementationMap& hm =
  6959. (implFor == LinkInterfaceFor::Usage
  6960. ? this->GetHeadToLinkImplementationUsageRequirementsMap(config)
  6961. : this->GetHeadToLinkImplementationMap(config));
  6962. cmOptionalLinkImplementation& impl = hm[this];
  6963. if (secondPass) {
  6964. impl = cmOptionalLinkImplementation();
  6965. }
  6966. if (!impl.LibrariesDone) {
  6967. impl.LibrariesDone = true;
  6968. this->ComputeLinkImplementationLibraries(config, impl, this, implFor);
  6969. }
  6970. if (!impl.LanguagesDone) {
  6971. impl.LanguagesDone = true;
  6972. this->ComputeLinkImplementationLanguages(config, impl);
  6973. this->ComputeLinkImplementationRuntimeLibraries(config, impl);
  6974. }
  6975. return &impl;
  6976. }
  6977. cmGeneratorTarget::HeadToLinkImplementationMap&
  6978. cmGeneratorTarget::GetHeadToLinkImplementationMap(
  6979. std::string const& config) const
  6980. {
  6981. return this->LinkImplMap[cmSystemTools::UpperCase(config)];
  6982. }
  6983. cmGeneratorTarget::HeadToLinkImplementationMap&
  6984. cmGeneratorTarget::GetHeadToLinkImplementationUsageRequirementsMap(
  6985. std::string const& config) const
  6986. {
  6987. return this
  6988. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6989. }
  6990. bool cmGeneratorTarget::GetConfigCommonSourceFilesForXcode(
  6991. std::vector<cmSourceFile*>& files) const
  6992. {
  6993. std::vector<std::string> const& configs =
  6994. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  6995. auto it = configs.begin();
  6996. const std::string& firstConfig = *it;
  6997. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  6998. for (; it != configs.end(); ++it) {
  6999. std::vector<cmSourceFile*> configFiles;
  7000. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  7001. if (configFiles != files) {
  7002. std::string firstConfigFiles;
  7003. const char* sep = "";
  7004. for (cmSourceFile* f : files) {
  7005. firstConfigFiles += sep;
  7006. firstConfigFiles += f->ResolveFullPath();
  7007. sep = "\n ";
  7008. }
  7009. std::string thisConfigFiles;
  7010. sep = "";
  7011. for (cmSourceFile* f : configFiles) {
  7012. thisConfigFiles += sep;
  7013. thisConfigFiles += f->ResolveFullPath();
  7014. sep = "\n ";
  7015. }
  7016. std::ostringstream e;
  7017. /* clang-format off */
  7018. e << "Target \"" << this->GetName()
  7019. << "\" has source files which vary by "
  7020. "configuration. This is not supported by the \""
  7021. << this->GlobalGenerator->GetName()
  7022. << "\" generator.\n"
  7023. "Config \"" << firstConfig << "\":\n"
  7024. " " << firstConfigFiles << "\n"
  7025. "Config \"" << *it << "\":\n"
  7026. " " << thisConfigFiles << "\n";
  7027. /* clang-format on */
  7028. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  7029. return false;
  7030. }
  7031. }
  7032. return true;
  7033. }
  7034. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  7035. std::vector<cmGeneratorTarget*>& objlibs) const
  7036. {
  7037. // At configure-time, this method can be called as part of getting the
  7038. // LOCATION property or to export() a file to be include()d. However
  7039. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  7040. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  7041. // behavior of CMP0024 and CMP0026 only.
  7042. cmBTStringRange rng = this->Target->GetSourceEntries();
  7043. for (auto const& entry : rng) {
  7044. cmList files{ entry.Value };
  7045. for (auto const& li : files) {
  7046. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  7047. std::string objLibName = li.substr(17, li.size() - 18);
  7048. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  7049. continue;
  7050. }
  7051. cmGeneratorTarget* objLib =
  7052. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  7053. if (objLib) {
  7054. objlibs.push_back(objLib);
  7055. }
  7056. }
  7057. }
  7058. }
  7059. }
  7060. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  7061. {
  7062. // Strip whitespace off the library names because we used to do this
  7063. // in case variables were expanded at generate time. We no longer
  7064. // do the expansion but users link to libraries like " ${VAR} ".
  7065. std::string lib = item;
  7066. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  7067. if (pos != std::string::npos) {
  7068. lib = lib.substr(pos);
  7069. }
  7070. pos = lib.find_last_not_of(" \t\r\n");
  7071. if (pos != std::string::npos) {
  7072. lib = lib.substr(0, pos + 1);
  7073. }
  7074. if (lib != item) {
  7075. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  7076. switch (this->GetPolicyStatusCMP0004()) {
  7077. case cmPolicies::WARN: {
  7078. std::ostringstream w;
  7079. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  7080. << "Target \"" << this->GetName() << "\" links to item \"" << item
  7081. << "\" which has leading or trailing whitespace.";
  7082. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  7083. this->GetBacktrace());
  7084. }
  7085. CM_FALLTHROUGH;
  7086. case cmPolicies::OLD:
  7087. break;
  7088. case cmPolicies::NEW: {
  7089. std::ostringstream e;
  7090. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  7091. << "\" which has leading or trailing whitespace. "
  7092. << "This is now an error according to policy CMP0004.";
  7093. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  7094. this->GetBacktrace());
  7095. } break;
  7096. case cmPolicies::REQUIRED_IF_USED:
  7097. case cmPolicies::REQUIRED_ALWAYS: {
  7098. std::ostringstream e;
  7099. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  7100. << "Target \"" << this->GetName() << "\" links to item \"" << item
  7101. << "\" which has leading or trailing whitespace.";
  7102. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  7103. this->GetBacktrace());
  7104. } break;
  7105. }
  7106. }
  7107. return lib;
  7108. }
  7109. bool cmGeneratorTarget::IsDeprecated() const
  7110. {
  7111. cmValue deprecation = this->GetProperty("DEPRECATION");
  7112. return cmNonempty(deprecation);
  7113. }
  7114. std::string cmGeneratorTarget::GetDeprecation() const
  7115. {
  7116. // find DEPRECATION property
  7117. if (cmValue deprecation = this->GetProperty("DEPRECATION")) {
  7118. return *deprecation;
  7119. }
  7120. return std::string();
  7121. }
  7122. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  7123. const std::string& config) const
  7124. {
  7125. // Targets that do not compile anything have no languages.
  7126. if (!this->CanCompileSources()) {
  7127. return;
  7128. }
  7129. std::vector<cmSourceFile*> sourceFiles;
  7130. this->GetSourceFiles(sourceFiles, config);
  7131. for (cmSourceFile* src : sourceFiles) {
  7132. const std::string& lang = src->GetOrDetermineLanguage();
  7133. if (!lang.empty()) {
  7134. languages.insert(lang);
  7135. }
  7136. }
  7137. std::vector<cmGeneratorTarget*> objectLibraries;
  7138. std::vector<cmSourceFile const*> externalObjects;
  7139. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  7140. std::vector<cmGeneratorTarget*> objectTargets;
  7141. this->GetObjectLibrariesCMP0026(objectTargets);
  7142. objectLibraries.reserve(objectTargets.size());
  7143. for (cmGeneratorTarget* gt : objectTargets) {
  7144. objectLibraries.push_back(gt);
  7145. }
  7146. } else {
  7147. this->GetExternalObjects(externalObjects, config);
  7148. for (cmSourceFile const* extObj : externalObjects) {
  7149. std::string objLib = extObj->GetObjectLibrary();
  7150. if (cmGeneratorTarget* tgt =
  7151. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  7152. auto const objLibIt =
  7153. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  7154. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  7155. if (objectLibraries.cend() == objLibIt) {
  7156. objectLibraries.push_back(tgt);
  7157. }
  7158. }
  7159. }
  7160. }
  7161. for (cmGeneratorTarget* objLib : objectLibraries) {
  7162. objLib->GetLanguages(languages, config);
  7163. }
  7164. }
  7165. bool cmGeneratorTarget::IsLanguageUsed(std::string const& language,
  7166. std::string const& config) const
  7167. {
  7168. std::set<std::string> languages;
  7169. this->GetLanguages(languages, config);
  7170. return languages.count(language);
  7171. }
  7172. bool cmGeneratorTarget::IsCSharpOnly() const
  7173. {
  7174. // Only certain target types may compile CSharp.
  7175. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  7176. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  7177. this->GetType() != cmStateEnums::EXECUTABLE) {
  7178. return false;
  7179. }
  7180. std::set<std::string> languages = this->GetAllConfigCompileLanguages();
  7181. // Consider an explicit linker language property, but *not* the
  7182. // computed linker language that may depend on linked targets.
  7183. cmValue linkLang = this->GetProperty("LINKER_LANGUAGE");
  7184. if (cmNonempty(linkLang)) {
  7185. languages.insert(*linkLang);
  7186. }
  7187. return languages.size() == 1 && languages.count("CSharp") > 0;
  7188. }
  7189. bool cmGeneratorTarget::IsDotNetSdkTarget() const
  7190. {
  7191. return !this->GetProperty("DOTNET_SDK").IsEmpty();
  7192. }
  7193. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  7194. const std::string& config, cmOptionalLinkImplementation& impl) const
  7195. {
  7196. // This target needs runtime libraries for its source languages.
  7197. std::set<std::string> languages;
  7198. // Get languages used in our source files.
  7199. this->GetLanguages(languages, config);
  7200. // Copy the set of languages to the link implementation.
  7201. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  7202. languages.end());
  7203. }
  7204. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  7205. {
  7206. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  7207. return false;
  7208. }
  7209. std::string build_rpath;
  7210. if (this->GetBuildRPATH(config, build_rpath)) {
  7211. return true;
  7212. }
  7213. if (cmLinkImplementationLibraries const* impl =
  7214. this->GetLinkImplementationLibraries(config, LinkInterfaceFor::Link)) {
  7215. return !impl->Libraries.empty();
  7216. }
  7217. return false;
  7218. }
  7219. cmLinkImplementationLibraries const*
  7220. cmGeneratorTarget::GetLinkImplementationLibraries(
  7221. const std::string& config, LinkInterfaceFor implFor) const
  7222. {
  7223. return this->GetLinkImplementationLibrariesInternal(config, this, implFor);
  7224. }
  7225. cmLinkImplementationLibraries const*
  7226. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  7227. const std::string& config, cmGeneratorTarget const* head,
  7228. LinkInterfaceFor implFor) const
  7229. {
  7230. // There is no link implementation for targets that cannot compile sources.
  7231. if (!this->CanCompileSources()) {
  7232. return nullptr;
  7233. }
  7234. // Populate the link implementation libraries for this configuration.
  7235. HeadToLinkImplementationMap& hm =
  7236. (implFor == LinkInterfaceFor::Usage
  7237. ? this->GetHeadToLinkImplementationUsageRequirementsMap(config)
  7238. : this->GetHeadToLinkImplementationMap(config));
  7239. // If the link implementation does not depend on the head target
  7240. // then re-use the one from the head we computed first.
  7241. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  7242. head = hm.begin()->first;
  7243. }
  7244. cmOptionalLinkImplementation& impl = hm[head];
  7245. if (!impl.LibrariesDone) {
  7246. impl.LibrariesDone = true;
  7247. this->ComputeLinkImplementationLibraries(config, impl, head, implFor);
  7248. }
  7249. return &impl;
  7250. }
  7251. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  7252. const std::string& p) const
  7253. {
  7254. return cm::contains(this->LinkImplicitNullProperties, p);
  7255. }
  7256. namespace {
  7257. class TransitiveLinkImpl
  7258. {
  7259. cmGeneratorTarget const* Self;
  7260. std::string const& Config;
  7261. LinkInterfaceFor ImplFor;
  7262. cmLinkImplementation& Impl;
  7263. std::set<cmLinkItem> Emitted;
  7264. std::set<cmLinkItem> Excluded;
  7265. std::unordered_set<cmGeneratorTarget const*> Followed;
  7266. void Follow(cmGeneratorTarget const* target);
  7267. public:
  7268. TransitiveLinkImpl(cmGeneratorTarget const* self, std::string const& config,
  7269. LinkInterfaceFor implFor, cmLinkImplementation& impl)
  7270. : Self(self)
  7271. , Config(config)
  7272. , ImplFor(implFor)
  7273. , Impl(impl)
  7274. {
  7275. }
  7276. void Compute();
  7277. };
  7278. void TransitiveLinkImpl::Follow(cmGeneratorTarget const* target)
  7279. {
  7280. if (!target || !this->Followed.insert(target).second ||
  7281. target->GetPolicyStatusCMP0022() == cmPolicies::OLD ||
  7282. target->GetPolicyStatusCMP0022() == cmPolicies::WARN) {
  7283. return;
  7284. }
  7285. // Get this target's usage requirements.
  7286. cmLinkInterfaceLibraries const* iface =
  7287. target->GetLinkInterfaceLibraries(this->Config, this->Self, this->ImplFor);
  7288. if (!iface) {
  7289. return;
  7290. }
  7291. if (iface->HadContextSensitiveCondition) {
  7292. this->Impl.HadContextSensitiveCondition = true;
  7293. }
  7294. // Process 'INTERFACE_LINK_LIBRARIES_DIRECT' usage requirements.
  7295. for (cmLinkItem const& item : iface->HeadInclude) {
  7296. // Inject direct dependencies from the item's usage requirements
  7297. // before the item itself.
  7298. this->Follow(item.Target);
  7299. // Add the item itself, but at most once.
  7300. if (this->Emitted.insert(item).second) {
  7301. this->Impl.Libraries.emplace_back(item, /* checkCMP0027= */ false);
  7302. }
  7303. }
  7304. // Follow transitive dependencies.
  7305. for (cmLinkItem const& item : iface->Libraries) {
  7306. this->Follow(item.Target);
  7307. }
  7308. // Record exclusions from 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  7309. // usage requirements.
  7310. for (cmLinkItem const& item : iface->HeadExclude) {
  7311. this->Excluded.insert(item);
  7312. }
  7313. }
  7314. void TransitiveLinkImpl::Compute()
  7315. {
  7316. // Save the original items and start with an empty list.
  7317. std::vector<cmLinkImplItem> original = std::move(this->Impl.Libraries);
  7318. // Avoid injecting any original items as usage requirements.
  7319. // This gives LINK_LIBRARIES final control over the order
  7320. // if it explicitly lists everything.
  7321. this->Emitted.insert(original.cbegin(), original.cend());
  7322. // Process each original item.
  7323. for (cmLinkImplItem& item : original) {
  7324. // Inject direct dependencies listed in 'INTERFACE_LINK_LIBRARIES_DIRECT'
  7325. // usage requirements before the item itself.
  7326. this->Follow(item.Target);
  7327. // Add the item itself.
  7328. this->Impl.Libraries.emplace_back(std::move(item));
  7329. }
  7330. // Remove items listed in 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  7331. // usage requirements found through any dependency above.
  7332. this->Impl.Libraries.erase(
  7333. std::remove_if(this->Impl.Libraries.begin(), this->Impl.Libraries.end(),
  7334. [this](cmLinkImplItem const& item) {
  7335. return this->Excluded.find(item) != this->Excluded.end();
  7336. }),
  7337. this->Impl.Libraries.end());
  7338. }
  7339. void ComputeLinkImplTransitive(cmGeneratorTarget const* self,
  7340. std::string const& config,
  7341. LinkInterfaceFor implFor,
  7342. cmLinkImplementation& impl)
  7343. {
  7344. TransitiveLinkImpl transitiveLinkImpl(self, config, implFor, impl);
  7345. transitiveLinkImpl.Compute();
  7346. }
  7347. }
  7348. bool cmGeneratorTarget::DiscoverSyntheticTargets(cmSyntheticTargetCache& cache,
  7349. std::string const& config)
  7350. {
  7351. cmOptionalLinkImplementation impl;
  7352. this->ComputeLinkImplementationLibraries(config, impl, this,
  7353. LinkInterfaceFor::Link);
  7354. cmCxxModuleUsageEffects usage(this);
  7355. auto& SyntheticDeps = this->Configs[config].SyntheticDeps;
  7356. for (auto const& entry : impl.Libraries) {
  7357. auto const* gt = entry.Target;
  7358. if (!gt || !gt->IsImported()) {
  7359. continue;
  7360. }
  7361. if (gt->HaveCxx20ModuleSources()) {
  7362. auto hasher = cmCryptoHash::New("SHA3_512");
  7363. constexpr size_t HASH_TRUNCATION = 12;
  7364. auto dirhash = hasher->HashString(
  7365. gt->GetLocalGenerator()->GetCurrentBinaryDirectory());
  7366. std::string safeName = gt->GetName();
  7367. cmSystemTools::ReplaceString(safeName, ":", "_");
  7368. auto targetIdent =
  7369. hasher->HashString(cmStrCat("@d_", dirhash, "@u_", usage.GetHash()));
  7370. std::string targetName =
  7371. cmStrCat(safeName, "@synth_", targetIdent.substr(0, HASH_TRUNCATION));
  7372. // Check the cache to see if this instance of the imported target has
  7373. // already been created.
  7374. auto cached = cache.CxxModuleTargets.find(targetName);
  7375. cmGeneratorTarget const* synthDep = nullptr;
  7376. if (cached == cache.CxxModuleTargets.end()) {
  7377. auto const* model = gt->Target;
  7378. auto* mf = gt->Makefile;
  7379. auto* lg = gt->GetLocalGenerator();
  7380. auto* tgt = mf->AddSynthesizedTarget(cmStateEnums::INTERFACE_LIBRARY,
  7381. targetName);
  7382. // Copy relevant information from the existing IMPORTED target.
  7383. // Copy policies to the target.
  7384. tgt->CopyPolicyStatuses(model);
  7385. // Copy file sets.
  7386. {
  7387. auto fsNames = model->GetAllFileSetNames();
  7388. for (auto const& fsName : fsNames) {
  7389. auto const* fs = model->GetFileSet(fsName);
  7390. if (!fs) {
  7391. mf->IssueMessage(MessageType::INTERNAL_ERROR,
  7392. cmStrCat("Failed to find file set named '",
  7393. fsName, "' on target '",
  7394. tgt->GetName(), '\''));
  7395. continue;
  7396. }
  7397. auto* newFs = tgt
  7398. ->GetOrCreateFileSet(fs->GetName(), fs->GetType(),
  7399. fs->GetVisibility())
  7400. .first;
  7401. newFs->CopyEntries(fs);
  7402. }
  7403. }
  7404. // Copy imported C++ module properties.
  7405. tgt->CopyImportedCxxModulesEntries(model);
  7406. // Copy other properties which may affect the C++ module BMI
  7407. // generation.
  7408. tgt->CopyImportedCxxModulesProperties(model);
  7409. // Apply usage requirements to the target.
  7410. usage.ApplyToTarget(tgt);
  7411. // Create the generator target and attach it to the local generator.
  7412. auto gtp = cm::make_unique<cmGeneratorTarget>(tgt, lg);
  7413. synthDep = gtp.get();
  7414. lg->AddGeneratorTarget(std::move(gtp));
  7415. } else {
  7416. synthDep = cached->second;
  7417. }
  7418. SyntheticDeps[gt].push_back(synthDep);
  7419. }
  7420. }
  7421. return true;
  7422. }
  7423. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  7424. const std::string& config, cmOptionalLinkImplementation& impl,
  7425. cmGeneratorTarget const* head, LinkInterfaceFor implFor) const
  7426. {
  7427. cmLocalGenerator const* lg = this->LocalGenerator;
  7428. cmMakefile const* mf = lg->GetMakefile();
  7429. cmBTStringRange entryRange = this->Target->GetLinkImplementationEntries();
  7430. auto const& synthTargetsForConfig = this->Configs[config].SyntheticDeps;
  7431. // Collect libraries directly linked in this configuration.
  7432. for (auto const& entry : entryRange) {
  7433. // Keep this logic in sync with ExpandLinkItems.
  7434. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  7435. nullptr);
  7436. // The $<LINK_ONLY> expression may be used to specify link dependencies
  7437. // that are otherwise excluded from usage requirements.
  7438. if (implFor == LinkInterfaceFor::Usage) {
  7439. dagChecker.SetTransitivePropertiesOnly();
  7440. switch (this->GetPolicyStatusCMP0131()) {
  7441. case cmPolicies::WARN:
  7442. case cmPolicies::OLD:
  7443. break;
  7444. case cmPolicies::REQUIRED_IF_USED:
  7445. case cmPolicies::REQUIRED_ALWAYS:
  7446. case cmPolicies::NEW:
  7447. dagChecker.SetTransitivePropertiesOnlyCMP0131();
  7448. break;
  7449. }
  7450. }
  7451. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  7452. entry.Backtrace);
  7453. std::unique_ptr<cmCompiledGeneratorExpression> const cge =
  7454. ge.Parse(entry.Value);
  7455. cge->SetEvaluateForBuildsystem(true);
  7456. std::string const& evaluated =
  7457. cge->Evaluate(this->LocalGenerator, config, head, &dagChecker, nullptr,
  7458. this->LinkerLanguage);
  7459. bool const checkCMP0027 = evaluated != entry.Value;
  7460. cmList llibs(evaluated);
  7461. if (cge->GetHadHeadSensitiveCondition()) {
  7462. impl.HadHeadSensitiveCondition = true;
  7463. }
  7464. if (cge->GetHadContextSensitiveCondition()) {
  7465. impl.HadContextSensitiveCondition = true;
  7466. }
  7467. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  7468. impl.HadLinkLanguageSensitiveCondition = true;
  7469. }
  7470. for (auto const& lib : llibs) {
  7471. if (this->IsLinkLookupScope(lib, lg)) {
  7472. continue;
  7473. }
  7474. // Skip entries that resolve to the target itself or are empty.
  7475. std::string name = this->CheckCMP0004(lib);
  7476. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  7477. // resolve alias name
  7478. auto* target = this->Makefile->FindTargetToUse(name);
  7479. if (target) {
  7480. name = target->GetName();
  7481. }
  7482. }
  7483. if (name == this->GetName() || name.empty()) {
  7484. if (name == this->GetName()) {
  7485. bool noMessage = false;
  7486. MessageType messageType = MessageType::FATAL_ERROR;
  7487. std::ostringstream e;
  7488. switch (this->GetPolicyStatusCMP0038()) {
  7489. case cmPolicies::WARN: {
  7490. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  7491. messageType = MessageType::AUTHOR_WARNING;
  7492. } break;
  7493. case cmPolicies::OLD:
  7494. noMessage = true;
  7495. break;
  7496. case cmPolicies::REQUIRED_IF_USED:
  7497. case cmPolicies::REQUIRED_ALWAYS:
  7498. case cmPolicies::NEW:
  7499. // Issue the fatal message.
  7500. break;
  7501. }
  7502. if (!noMessage) {
  7503. e << "Target \"" << this->GetName() << "\" links to itself.";
  7504. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  7505. messageType, e.str(), this->GetBacktrace());
  7506. if (messageType == MessageType::FATAL_ERROR) {
  7507. return;
  7508. }
  7509. }
  7510. }
  7511. continue;
  7512. }
  7513. // The entry is meant for this configuration.
  7514. cmLinkItem item =
  7515. this->ResolveLinkItem(BT<std::string>(name, entry.Backtrace), lg);
  7516. if (item.Target) {
  7517. auto depsForTarget = synthTargetsForConfig.find(item.Target);
  7518. if (depsForTarget != synthTargetsForConfig.end()) {
  7519. for (auto const* depForTarget : depsForTarget->second) {
  7520. cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace);
  7521. impl.Libraries.emplace_back(std::move(synthItem), false);
  7522. }
  7523. }
  7524. } else {
  7525. // Report explicitly linked object files separately.
  7526. std::string const& maybeObj = item.AsStr();
  7527. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  7528. cmSourceFile const* sf =
  7529. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  7530. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  7531. impl.Objects.emplace_back(std::move(item));
  7532. continue;
  7533. }
  7534. }
  7535. }
  7536. impl.Libraries.emplace_back(std::move(item), checkCMP0027);
  7537. }
  7538. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  7539. for (std::string const& sp : seenProps) {
  7540. if (!this->GetProperty(sp)) {
  7541. this->LinkImplicitNullProperties.insert(sp);
  7542. }
  7543. }
  7544. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  7545. }
  7546. // Update the list of direct link dependencies from usage requirements.
  7547. if (head == this) {
  7548. ComputeLinkImplTransitive(this, config, implFor, impl);
  7549. }
  7550. // Get the list of configurations considered to be DEBUG.
  7551. std::vector<std::string> debugConfigs =
  7552. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  7553. cmTargetLinkLibraryType linkType =
  7554. CMP0003_ComputeLinkType(config, debugConfigs);
  7555. cmTarget::LinkLibraryVectorType const& oldllibs =
  7556. this->Target->GetOriginalLinkLibraries();
  7557. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  7558. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  7559. std::string name = this->CheckCMP0004(oldllib.first);
  7560. if (name == this->GetName() || name.empty()) {
  7561. continue;
  7562. }
  7563. // Support OLD behavior for CMP0003.
  7564. impl.WrongConfigLibraries.push_back(
  7565. this->ResolveLinkItem(BT<std::string>(name)));
  7566. }
  7567. }
  7568. }
  7569. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  7570. std::string const& name) const
  7571. {
  7572. return this->ResolveTargetReference(name, this->LocalGenerator);
  7573. }
  7574. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  7575. std::string const& name, cmLocalGenerator const* lg) const
  7576. {
  7577. TargetOrString resolved;
  7578. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  7579. resolved.Target = tgt;
  7580. } else {
  7581. resolved.String = name;
  7582. }
  7583. return resolved;
  7584. }
  7585. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  7586. BT<std::string> const& name) const
  7587. {
  7588. return this->ResolveLinkItem(name, this->LocalGenerator);
  7589. }
  7590. cmLinkItem cmGeneratorTarget::ResolveLinkItem(BT<std::string> const& name,
  7591. cmLocalGenerator const* lg) const
  7592. {
  7593. auto bt = name.Backtrace;
  7594. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  7595. if (!resolved.Target) {
  7596. return cmLinkItem(resolved.String, false, bt);
  7597. }
  7598. // Check deprecation, issue message with `bt` backtrace.
  7599. if (resolved.Target->IsDeprecated()) {
  7600. std::ostringstream w;
  7601. /* clang-format off */
  7602. w <<
  7603. "The library that is being linked to, " << resolved.Target->GetName() <<
  7604. ", is marked as being deprecated by the owner. The message provided by "
  7605. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  7606. /* clang-format on */
  7607. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  7608. MessageType::AUTHOR_WARNING, w.str(), bt);
  7609. }
  7610. // Skip targets that will not really be linked. This is probably a
  7611. // name conflict between an external library and an executable
  7612. // within the project.
  7613. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  7614. !resolved.Target->IsExecutableWithExports()) {
  7615. return cmLinkItem(resolved.Target->GetName(), false, bt);
  7616. }
  7617. return cmLinkItem(resolved.Target, false, bt);
  7618. }
  7619. bool cmGeneratorTarget::HasPackageReferences() const
  7620. {
  7621. return this->IsInBuildSystem() &&
  7622. !this->GetProperty("VS_PACKAGE_REFERENCES")->empty();
  7623. }
  7624. std::vector<std::string> cmGeneratorTarget::GetPackageReferences() const
  7625. {
  7626. cmList packageReferences;
  7627. if (this->IsInBuildSystem()) {
  7628. if (cmValue vsPackageReferences =
  7629. this->GetProperty("VS_PACKAGE_REFERENCES")) {
  7630. packageReferences.assign(*vsPackageReferences);
  7631. }
  7632. }
  7633. return std::move(packageReferences.data());
  7634. }
  7635. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  7636. {
  7637. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  7638. // Return the directory in which the target will be built.
  7639. return info->PdbDir;
  7640. }
  7641. return "";
  7642. }
  7643. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  7644. {
  7645. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  7646. }
  7647. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  7648. std::string const& gnuName,
  7649. std::string& out,
  7650. const char* newExt) const
  7651. {
  7652. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  7653. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  7654. out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
  7655. newExt ? newExt : ".lib");
  7656. return true;
  7657. }
  7658. return false;
  7659. }
  7660. bool cmGeneratorTarget::HasContextDependentSources() const
  7661. {
  7662. return this->SourcesAreContextDependent == Tribool::True;
  7663. }
  7664. bool cmGeneratorTarget::IsExecutableWithExports() const
  7665. {
  7666. return this->Target->IsExecutableWithExports();
  7667. }
  7668. bool cmGeneratorTarget::IsSharedLibraryWithExports() const
  7669. {
  7670. return this->Target->IsSharedLibraryWithExports();
  7671. }
  7672. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  7673. {
  7674. bool generate_Stubs = true;
  7675. if (this->GetGlobalGenerator()->IsXcode()) {
  7676. // take care of CMAKE_XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS variable
  7677. // as well as XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS property
  7678. if (cmValue propGenStubs =
  7679. this->GetProperty("XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS")) {
  7680. generate_Stubs = propGenStubs == "YES";
  7681. } else if (cmValue varGenStubs = this->Makefile->GetDefinition(
  7682. "CMAKE_XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS")) {
  7683. generate_Stubs = varGenStubs == "YES";
  7684. }
  7685. }
  7686. return (this->IsDLLPlatform() &&
  7687. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7688. this->IsExecutableWithExports()) &&
  7689. // Assemblies which have only managed code do not have
  7690. // import libraries.
  7691. this->GetManagedType(config) != ManagedType::Managed) ||
  7692. (this->IsAIX() && this->IsExecutableWithExports()) ||
  7693. (this->Makefile->PlatformSupportsAppleTextStubs() &&
  7694. this->IsSharedLibraryWithExports() && generate_Stubs);
  7695. }
  7696. bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
  7697. {
  7698. return this->HasImportLibrary(config) ||
  7699. // On DLL platforms we always generate the import library name
  7700. // just in case the sources have export markup.
  7701. (this->IsDLLPlatform() &&
  7702. (this->GetType() == cmStateEnums::EXECUTABLE ||
  7703. this->GetType() == cmStateEnums::MODULE_LIBRARY));
  7704. }
  7705. std::string cmGeneratorTarget::GetSupportDirectory() const
  7706. {
  7707. std::string dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  7708. "/CMakeFiles/", this->GetName());
  7709. #if defined(__VMS)
  7710. dir += "_dir";
  7711. #else
  7712. dir += ".dir";
  7713. #endif
  7714. return dir;
  7715. }
  7716. bool cmGeneratorTarget::IsLinkable() const
  7717. {
  7718. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  7719. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7720. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  7721. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  7722. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  7723. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  7724. this->IsExecutableWithExports());
  7725. }
  7726. bool cmGeneratorTarget::HasLinkDependencyFile(std::string const& config) const
  7727. {
  7728. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  7729. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  7730. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  7731. return false;
  7732. }
  7733. if (this->Target->GetProperty("LINK_DEPENDS_NO_SHARED").IsOn()) {
  7734. // Do not use the linker dependency file because it includes shared
  7735. // libraries as well
  7736. return false;
  7737. }
  7738. const std::string depsUseLinker{ "CMAKE_LINK_DEPENDS_USE_LINKER" };
  7739. auto linkLanguage = this->GetLinkerLanguage(config);
  7740. const std::string langDepsUseLinker{ cmStrCat("CMAKE_", linkLanguage,
  7741. "_LINK_DEPENDS_USE_LINKER") };
  7742. return (!this->Makefile->IsDefinitionSet(depsUseLinker) ||
  7743. this->Makefile->IsOn(depsUseLinker)) &&
  7744. this->Makefile->IsOn(langDepsUseLinker);
  7745. }
  7746. bool cmGeneratorTarget::IsFrameworkOnApple() const
  7747. {
  7748. return this->Target->IsFrameworkOnApple();
  7749. }
  7750. bool cmGeneratorTarget::IsImportedFrameworkFolderOnApple(
  7751. const std::string& config) const
  7752. {
  7753. return this->IsApple() && this->IsImported() &&
  7754. (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  7755. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  7756. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY) &&
  7757. cmSystemTools::IsPathToFramework(this->GetLocation(config));
  7758. }
  7759. bool cmGeneratorTarget::IsAppBundleOnApple() const
  7760. {
  7761. return this->Target->IsAppBundleOnApple();
  7762. }
  7763. bool cmGeneratorTarget::IsXCTestOnApple() const
  7764. {
  7765. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  7766. }
  7767. bool cmGeneratorTarget::IsCFBundleOnApple() const
  7768. {
  7769. return (this->GetType() == cmStateEnums::MODULE_LIBRARY && this->IsApple() &&
  7770. this->GetPropertyAsBool("BUNDLE"));
  7771. }
  7772. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  7773. std::string const& propval) const
  7774. {
  7775. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  7776. // or only C++/CLI) does only depend on whether the property is an empty
  7777. // string or contains any value at all. In Visual Studio generators
  7778. // this propval is prepended with /clr[:] which results in:
  7779. //
  7780. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  7781. // lib
  7782. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  7783. // target, has import lib
  7784. // 3. netcore propval: add /clr:netcore as flag, mixed
  7785. // unmanaged/managed target, has import lib.
  7786. // 4. any value (safe,pure): add /clr:[propval] as flag, target with
  7787. // managed code only, no import lib
  7788. if (propval.empty() || propval == "netcore") {
  7789. return ManagedType::Mixed;
  7790. }
  7791. return ManagedType::Managed;
  7792. }
  7793. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  7794. const std::string& config) const
  7795. {
  7796. // Only libraries and executables can be managed targets.
  7797. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  7798. return ManagedType::Undefined;
  7799. }
  7800. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  7801. return ManagedType::Native;
  7802. }
  7803. // Check imported target.
  7804. if (this->IsImported()) {
  7805. if (cmGeneratorTarget::ImportInfo const* info =
  7806. this->GetImportInfo(config)) {
  7807. return info->Managed;
  7808. }
  7809. return ManagedType::Undefined;
  7810. }
  7811. // Check for explicitly set clr target property.
  7812. if (cmValue clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  7813. return this->CheckManagedType(*clr);
  7814. }
  7815. // C# targets are always managed. This language specific check
  7816. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  7817. // has to be set manually for C# targets.
  7818. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  7819. }
  7820. bool cmGeneratorTarget::AddHeaderSetVerification()
  7821. {
  7822. if (!this->GetPropertyAsBool("VERIFY_INTERFACE_HEADER_SETS")) {
  7823. return true;
  7824. }
  7825. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  7826. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  7827. this->GetType() != cmStateEnums::UNKNOWN_LIBRARY &&
  7828. this->GetType() != cmStateEnums::OBJECT_LIBRARY &&
  7829. this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  7830. !this->IsExecutableWithExports()) {
  7831. return true;
  7832. }
  7833. auto verifyValue = this->GetProperty("INTERFACE_HEADER_SETS_TO_VERIFY");
  7834. const bool all = verifyValue.IsEmpty();
  7835. std::set<std::string> verifySet;
  7836. if (!all) {
  7837. cmList verifyList{ verifyValue };
  7838. verifySet.insert(verifyList.begin(), verifyList.end());
  7839. }
  7840. cmTarget* verifyTarget = nullptr;
  7841. cmTarget* allVerifyTarget =
  7842. this->GlobalGenerator->GetMakefiles().front()->FindTargetToUse(
  7843. "all_verify_interface_header_sets", true);
  7844. auto interfaceFileSetEntries = this->Target->GetInterfaceHeaderSetsEntries();
  7845. std::set<cmFileSet*> fileSets;
  7846. for (auto const& entry : interfaceFileSetEntries) {
  7847. for (auto const& name : cmList{ entry.Value }) {
  7848. if (all || verifySet.count(name)) {
  7849. fileSets.insert(this->Target->GetFileSet(name));
  7850. verifySet.erase(name);
  7851. }
  7852. }
  7853. }
  7854. if (!verifySet.empty()) {
  7855. this->Makefile->IssueMessage(
  7856. MessageType::FATAL_ERROR,
  7857. cmStrCat("Property INTERFACE_HEADER_SETS_TO_VERIFY of target \"",
  7858. this->GetName(),
  7859. "\" contained the following header sets that are nonexistent "
  7860. "or not INTERFACE:\n ",
  7861. cmJoin(verifySet, "\n ")));
  7862. return false;
  7863. }
  7864. cm::optional<std::set<std::string>> languages;
  7865. for (auto* fileSet : fileSets) {
  7866. auto dirCges = fileSet->CompileDirectoryEntries();
  7867. auto fileCges = fileSet->CompileFileEntries();
  7868. static auto const contextSensitive =
  7869. [](const std::unique_ptr<cmCompiledGeneratorExpression>& cge) {
  7870. return cge->GetHadContextSensitiveCondition();
  7871. };
  7872. bool dirCgesContextSensitive = false;
  7873. bool fileCgesContextSensitive = false;
  7874. std::vector<std::string> dirs;
  7875. std::map<std::string, std::vector<std::string>> filesPerDir;
  7876. bool first = true;
  7877. for (auto const& config : this->Makefile->GetGeneratorConfigs(
  7878. cmMakefile::GeneratorConfigQuery::IncludeEmptyConfig)) {
  7879. if (first || dirCgesContextSensitive) {
  7880. dirs = fileSet->EvaluateDirectoryEntries(dirCges, this->LocalGenerator,
  7881. config, this);
  7882. dirCgesContextSensitive =
  7883. std::any_of(dirCges.begin(), dirCges.end(), contextSensitive);
  7884. }
  7885. if (first || fileCgesContextSensitive) {
  7886. filesPerDir.clear();
  7887. for (auto const& fileCge : fileCges) {
  7888. fileSet->EvaluateFileEntry(dirs, filesPerDir, fileCge,
  7889. this->LocalGenerator, config, this);
  7890. if (fileCge->GetHadContextSensitiveCondition()) {
  7891. fileCgesContextSensitive = true;
  7892. }
  7893. }
  7894. }
  7895. for (auto const& files : filesPerDir) {
  7896. for (auto const& file : files.second) {
  7897. std::string filename = this->GenerateHeaderSetVerificationFile(
  7898. *this->Makefile->GetOrCreateSource(file), files.first, languages);
  7899. if (filename.empty()) {
  7900. continue;
  7901. }
  7902. if (!verifyTarget) {
  7903. {
  7904. cmMakefile::PolicyPushPop polScope(this->Makefile);
  7905. this->Makefile->SetPolicy(cmPolicies::CMP0119, cmPolicies::NEW);
  7906. verifyTarget = this->Makefile->AddLibrary(
  7907. cmStrCat(this->GetName(), "_verify_interface_header_sets"),
  7908. cmStateEnums::OBJECT_LIBRARY, {}, true);
  7909. }
  7910. verifyTarget->AddLinkLibrary(
  7911. *this->Makefile, this->GetName(),
  7912. cmTargetLinkLibraryType::GENERAL_LibraryType);
  7913. verifyTarget->SetProperty("AUTOMOC", "OFF");
  7914. verifyTarget->SetProperty("AUTORCC", "OFF");
  7915. verifyTarget->SetProperty("AUTOUIC", "OFF");
  7916. verifyTarget->SetProperty("DISABLE_PRECOMPILE_HEADERS", "ON");
  7917. verifyTarget->SetProperty("UNITY_BUILD", "OFF");
  7918. cm::optional<std::map<std::string, cmValue>>
  7919. perConfigCompileDefinitions;
  7920. verifyTarget->FinalizeTargetConfiguration(
  7921. this->Makefile->GetCompileDefinitionsEntries(),
  7922. perConfigCompileDefinitions);
  7923. if (!allVerifyTarget) {
  7924. allVerifyTarget = this->GlobalGenerator->GetMakefiles()
  7925. .front()
  7926. ->AddNewUtilityTarget(
  7927. "all_verify_interface_header_sets", true);
  7928. }
  7929. allVerifyTarget->AddUtility(verifyTarget->GetName(), false);
  7930. }
  7931. if (fileCgesContextSensitive) {
  7932. filename = cmStrCat("$<$<CONFIG:", config, ">:", filename, ">");
  7933. }
  7934. verifyTarget->AddSource(filename);
  7935. }
  7936. }
  7937. if (!dirCgesContextSensitive && !fileCgesContextSensitive) {
  7938. break;
  7939. }
  7940. first = false;
  7941. }
  7942. }
  7943. if (verifyTarget) {
  7944. this->LocalGenerator->AddGeneratorTarget(
  7945. cm::make_unique<cmGeneratorTarget>(verifyTarget, this->LocalGenerator));
  7946. }
  7947. return true;
  7948. }
  7949. std::string cmGeneratorTarget::GenerateHeaderSetVerificationFile(
  7950. cmSourceFile& source, const std::string& dir,
  7951. cm::optional<std::set<std::string>>& languages) const
  7952. {
  7953. std::string extension;
  7954. std::string language = source.GetOrDetermineLanguage();
  7955. if (source.GetPropertyAsBool("SKIP_LINTING")) {
  7956. return std::string{};
  7957. }
  7958. if (language.empty()) {
  7959. if (!languages) {
  7960. languages.emplace();
  7961. for (auto const& tgtSource : this->GetAllConfigSources()) {
  7962. auto const& tgtSourceLanguage =
  7963. tgtSource.Source->GetOrDetermineLanguage();
  7964. if (tgtSourceLanguage == "CXX") {
  7965. languages->insert("CXX");
  7966. break; // C++ overrides everything else, so we don't need to keep
  7967. // checking.
  7968. }
  7969. if (tgtSourceLanguage == "C") {
  7970. languages->insert("C");
  7971. }
  7972. }
  7973. if (languages->empty()) {
  7974. std::vector<std::string> languagesVector;
  7975. this->GlobalGenerator->GetEnabledLanguages(languagesVector);
  7976. languages->insert(languagesVector.begin(), languagesVector.end());
  7977. }
  7978. }
  7979. if (languages->count("CXX")) {
  7980. language = "CXX";
  7981. } else if (languages->count("C")) {
  7982. language = "C";
  7983. }
  7984. }
  7985. if (language == "C") {
  7986. extension = ".c";
  7987. } else if (language == "CXX") {
  7988. extension = ".cxx";
  7989. } else {
  7990. return "";
  7991. }
  7992. std::string headerFilename = dir;
  7993. if (!headerFilename.empty()) {
  7994. headerFilename += '/';
  7995. }
  7996. headerFilename += source.GetLocation().GetName();
  7997. auto filename = cmStrCat(
  7998. this->LocalGenerator->GetCurrentBinaryDirectory(), '/', this->GetName(),
  7999. "_verify_interface_header_sets/", headerFilename, extension);
  8000. auto* verificationSource = this->Makefile->GetOrCreateSource(filename);
  8001. verificationSource->SetProperty("LANGUAGE", language);
  8002. cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(filename));
  8003. cmGeneratedFileStream fout(filename);
  8004. fout.SetCopyIfDifferent(true);
  8005. // The IWYU "associated" pragma tells include-what-you-use to
  8006. // consider the headerFile as part of the entire language
  8007. // unit within include-what-you-use and as a result allows
  8008. // one to get IWYU advice for headers.
  8009. fout << "#include <" << headerFilename << "> // IWYU pragma: associated\n";
  8010. fout.close();
  8011. return filename;
  8012. }
  8013. std::string cmGeneratorTarget::GetImportedXcFrameworkPath(
  8014. const std::string& config) const
  8015. {
  8016. if (!(this->IsApple() && this->IsImported() &&
  8017. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  8018. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  8019. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY))) {
  8020. return {};
  8021. }
  8022. std::string desiredConfig = config;
  8023. if (config.empty()) {
  8024. desiredConfig = "NOCONFIG";
  8025. }
  8026. std::string result;
  8027. cmValue loc = nullptr;
  8028. cmValue imp = nullptr;
  8029. std::string suffix;
  8030. if (this->Target->GetMappedConfig(desiredConfig, loc, imp, suffix)) {
  8031. if (loc) {
  8032. result = *loc;
  8033. } else {
  8034. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  8035. if (cmValue configLocation = this->GetProperty(impProp)) {
  8036. result = *configLocation;
  8037. } else if (cmValue location = this->GetProperty("IMPORTED_LOCATION")) {
  8038. result = *location;
  8039. }
  8040. }
  8041. if (cmSystemTools::IsPathToXcFramework(result)) {
  8042. return result;
  8043. }
  8044. }
  8045. return {};
  8046. }
  8047. bool cmGeneratorTarget::HaveFortranSources(std::string const& config) const
  8048. {
  8049. auto sources = this->GetSourceFiles(config);
  8050. return std::any_of(sources.begin(), sources.end(),
  8051. [](BT<cmSourceFile*> const& sf) -> bool {
  8052. return sf.Value->GetLanguage() == "Fortran"_s;
  8053. });
  8054. }
  8055. bool cmGeneratorTarget::HaveFortranSources() const
  8056. {
  8057. auto sources = cmGeneratorTarget::GetAllConfigSources();
  8058. return std::any_of(sources.begin(), sources.end(),
  8059. [](AllConfigSource const& sf) -> bool {
  8060. return sf.Source->GetLanguage() == "Fortran"_s;
  8061. });
  8062. }
  8063. bool cmGeneratorTarget::HaveCxx20ModuleSources(std::string* errorMessage) const
  8064. {
  8065. auto const& fs_names = this->Target->GetAllFileSetNames();
  8066. return std::any_of(
  8067. fs_names.begin(), fs_names.end(),
  8068. [this, errorMessage](std::string const& name) -> bool {
  8069. auto const* file_set = this->Target->GetFileSet(name);
  8070. if (!file_set) {
  8071. auto message = cmStrCat("Target \"", this->Target->GetName(),
  8072. "\" is tracked to have file set \"", name,
  8073. "\", but it was not found.");
  8074. if (errorMessage) {
  8075. *errorMessage = std::move(message);
  8076. } else {
  8077. this->Makefile->IssueMessage(MessageType::INTERNAL_ERROR, message);
  8078. }
  8079. return false;
  8080. }
  8081. auto const& fs_type = file_set->GetType();
  8082. return fs_type == "CXX_MODULES"_s;
  8083. });
  8084. }
  8085. cmGeneratorTarget::Cxx20SupportLevel cmGeneratorTarget::HaveCxxModuleSupport(
  8086. std::string const& config) const
  8087. {
  8088. auto const* state = this->Makefile->GetState();
  8089. if (!state->GetLanguageEnabled("CXX")) {
  8090. return Cxx20SupportLevel::MissingCxx;
  8091. }
  8092. cmValue standardDefault =
  8093. this->Target->GetMakefile()->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
  8094. if (standardDefault && !standardDefault->empty()) {
  8095. cmStandardLevelResolver standardResolver(this->Makefile);
  8096. if (!standardResolver.HaveStandardAvailable(this, "CXX", config,
  8097. "cxx_std_20")) {
  8098. return Cxx20SupportLevel::NoCxx20;
  8099. }
  8100. }
  8101. // Else, an empty CMAKE_CXX_STANDARD_DEFAULT means CMake does not detect and
  8102. // set a default standard level for this compiler, so assume all standards
  8103. // are available.
  8104. cmValue scandepRule =
  8105. this->Target->GetMakefile()->GetDefinition("CMAKE_CXX_SCANDEP_SOURCE");
  8106. if (!scandepRule) {
  8107. return Cxx20SupportLevel::MissingRule;
  8108. }
  8109. return Cxx20SupportLevel::Supported;
  8110. }
  8111. void cmGeneratorTarget::CheckCxxModuleStatus(std::string const& config) const
  8112. {
  8113. bool haveScannableSources = false;
  8114. // Check for `CXX_MODULE*` file sets and a lack of support.
  8115. if (this->HaveCxx20ModuleSources()) {
  8116. haveScannableSources = true;
  8117. }
  8118. if (!haveScannableSources) {
  8119. // Check to see if there are regular sources that have requested scanning.
  8120. auto sources = this->GetSourceFiles(config);
  8121. for (auto const& source : sources) {
  8122. auto const* sf = source.Value;
  8123. auto const& lang = sf->GetLanguage();
  8124. if (lang != "CXX"_s) {
  8125. continue;
  8126. }
  8127. // Ignore sources which do not need dyndep.
  8128. if (this->NeedDyndepForSource(lang, config, sf)) {
  8129. haveScannableSources = true;
  8130. }
  8131. }
  8132. }
  8133. // If there isn't anything scannable, ignore it.
  8134. if (!haveScannableSources) {
  8135. return;
  8136. }
  8137. // If the generator doesn't support modules at all, error that we have
  8138. // sources that require the support.
  8139. if (!this->GetGlobalGenerator()->CheckCxxModuleSupport()) {
  8140. this->Makefile->IssueMessage(
  8141. MessageType::FATAL_ERROR,
  8142. cmStrCat(
  8143. "The target named \"", this->GetName(),
  8144. "\" contains C++ "
  8145. "sources that use modules which is not supported by the generator"));
  8146. return;
  8147. }
  8148. switch (this->HaveCxxModuleSupport(config)) {
  8149. case cmGeneratorTarget::Cxx20SupportLevel::MissingCxx:
  8150. this->Makefile->IssueMessage(
  8151. MessageType::FATAL_ERROR,
  8152. cmStrCat("The target named \"", this->GetName(),
  8153. "\" has C++ sources that use modules but the \"CXX\" "
  8154. "language has not been enabled"));
  8155. break;
  8156. case cmGeneratorTarget::Cxx20SupportLevel::NoCxx20: {
  8157. cmStandardLevelResolver standardResolver(this->Makefile);
  8158. auto effStandard =
  8159. standardResolver.GetEffectiveStandard(this, "CXX", config);
  8160. if (effStandard.empty()) {
  8161. effStandard = "; no C++ standard found";
  8162. } else {
  8163. effStandard = cmStrCat("; found \"cxx_std_", effStandard, '"');
  8164. }
  8165. this->Makefile->IssueMessage(
  8166. MessageType::FATAL_ERROR,
  8167. cmStrCat(
  8168. "The target named \"", this->GetName(),
  8169. "\" has C++ sources that use modules but does not include "
  8170. "\"cxx_std_20\" (or newer) among its `target_compile_features`",
  8171. effStandard));
  8172. } break;
  8173. case cmGeneratorTarget::Cxx20SupportLevel::MissingRule: {
  8174. this->Makefile->IssueMessage(
  8175. MessageType::FATAL_ERROR,
  8176. cmStrCat("The target named \"", this->GetName(),
  8177. "\" has C++ sources that use modules but the compiler does "
  8178. "not provide a way to discover the import graph "
  8179. "dependencies"));
  8180. } break;
  8181. case cmGeneratorTarget::Cxx20SupportLevel::Supported:
  8182. // All is well.
  8183. break;
  8184. }
  8185. }
  8186. bool cmGeneratorTarget::NeedCxxModuleSupport(std::string const& lang,
  8187. std::string const& config) const
  8188. {
  8189. if (lang != "CXX"_s) {
  8190. return false;
  8191. }
  8192. return this->HaveCxxModuleSupport(config) == Cxx20SupportLevel::Supported &&
  8193. this->GetGlobalGenerator()->CheckCxxModuleSupport();
  8194. }
  8195. bool cmGeneratorTarget::NeedDyndep(std::string const& lang,
  8196. std::string const& config) const
  8197. {
  8198. return lang == "Fortran"_s || this->NeedCxxModuleSupport(lang, config);
  8199. }
  8200. cmFileSet const* cmGeneratorTarget::GetFileSetForSource(
  8201. std::string const& config, cmSourceFile const* sf) const
  8202. {
  8203. this->BuildFileSetInfoCache(config);
  8204. auto const& path = sf->GetFullPath();
  8205. auto const& per_config = this->Configs[config];
  8206. auto const fsit = per_config.FileSetCache.find(path);
  8207. if (fsit == per_config.FileSetCache.end()) {
  8208. return nullptr;
  8209. }
  8210. return fsit->second;
  8211. }
  8212. bool cmGeneratorTarget::NeedDyndepForSource(std::string const& lang,
  8213. std::string const& config,
  8214. cmSourceFile const* sf) const
  8215. {
  8216. // Fortran always needs to be scanned.
  8217. if (lang == "Fortran"_s) {
  8218. return true;
  8219. }
  8220. // Only C++ code needs scanned otherwise.
  8221. if (lang != "CXX"_s) {
  8222. return false;
  8223. }
  8224. // Any file in `CXX_MODULES` file sets need scanned (it being `CXX` is
  8225. // enforced elsewhere).
  8226. auto const* fs = this->GetFileSetForSource(config, sf);
  8227. if (fs && fs->GetType() == "CXX_MODULES"_s) {
  8228. return true;
  8229. }
  8230. switch (this->HaveCxxModuleSupport(config)) {
  8231. case Cxx20SupportLevel::MissingCxx:
  8232. case Cxx20SupportLevel::NoCxx20:
  8233. return false;
  8234. case Cxx20SupportLevel::MissingRule:
  8235. case Cxx20SupportLevel::Supported:
  8236. break;
  8237. }
  8238. auto const sfProp = sf->GetProperty("CXX_SCAN_FOR_MODULES");
  8239. if (sfProp.IsSet()) {
  8240. return sfProp.IsOn();
  8241. }
  8242. auto const tgtProp = this->GetProperty("CXX_SCAN_FOR_MODULES");
  8243. if (tgtProp.IsSet()) {
  8244. return tgtProp.IsOn();
  8245. }
  8246. return true;
  8247. }
  8248. void cmGeneratorTarget::BuildFileSetInfoCache(std::string const& config) const
  8249. {
  8250. auto& per_config = this->Configs[config];
  8251. if (per_config.BuiltFileSetCache) {
  8252. return;
  8253. }
  8254. auto const* tgt = this->Target;
  8255. for (auto const& name : tgt->GetAllFileSetNames()) {
  8256. auto const* file_set = tgt->GetFileSet(name);
  8257. if (!file_set) {
  8258. tgt->GetMakefile()->IssueMessage(
  8259. MessageType::INTERNAL_ERROR,
  8260. cmStrCat("Target \"", tgt->GetName(),
  8261. "\" is tracked to have file set \"", name,
  8262. "\", but it was not found."));
  8263. continue;
  8264. }
  8265. auto fileEntries = file_set->CompileFileEntries();
  8266. auto directoryEntries = file_set->CompileDirectoryEntries();
  8267. auto directories = file_set->EvaluateDirectoryEntries(
  8268. directoryEntries, this->LocalGenerator, config, this);
  8269. std::map<std::string, std::vector<std::string>> files;
  8270. for (auto const& entry : fileEntries) {
  8271. file_set->EvaluateFileEntry(directories, files, entry,
  8272. this->LocalGenerator, config, this);
  8273. }
  8274. for (auto const& it : files) {
  8275. for (auto const& filename : it.second) {
  8276. per_config.FileSetCache[filename] = file_set;
  8277. }
  8278. }
  8279. }
  8280. per_config.BuiltFileSetCache = true;
  8281. }