cmGeneratorTarget.cxx 275 KB

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