cmGeneratorTarget.cxx 249 KB

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