cmGeneratorTarget.cxx 216 KB

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