cmGeneratorTarget.cxx 244 KB

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