cmGeneratorTarget.cxx 222 KB

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