cmGeneratorTarget.cxx 248 KB

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