cmGeneratorTarget.cxx 221 KB

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