cmGeneratorTarget.cxx 237 KB

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