cmGeneratorTarget.cxx 307 KB

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