cmGeneratorTarget.cxx 226 KB

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