cmGeneratorTarget.cxx 225 KB

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