cmGeneratorTarget.cxx 302 KB

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