cmGeneratorTarget.cxx 227 KB

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