cmGeneratorTarget.cxx 220 KB

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