cmGeneratorTarget.cxx 255 KB

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