cmGeneratorTarget.cxx 308 KB

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