cmGeneratorTarget.cxx 245 KB

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