cmGeneratorTarget.cxx 275 KB

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