cmGeneratorTarget.cxx 319 KB

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