cmGeneratorTarget.cxx 232 KB

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