cmGeneratorTarget.cxx 225 KB

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