cmGeneratorTarget.cxx 242 KB

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