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