cmGeneratorTarget.cxx 218 KB

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