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