cmGeneratorTarget.cxx 254 KB

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