cmGeneratorTarget.cxx 328 KB

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