cmGeneratorTarget.cxx 281 KB

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